Skip to content

aaronwlee/Attain-GraphQL

Repository files navigation

Attain-GraphQL

A simple graphql middleware for Attain deno framework.

alt text

Simple example

import {
  App,
  Request,
  Response,
  logger,
  parser,
} from "https://deno.land/x/attain/mod.ts";
import { applyGraphQL, gql } from "https://deno.land/x/attain_graphql/mod.ts";

const app = new App();

app.use(logger);
app.use(parser);

const types = gql`
type User {
  firstName: String
  lastName: String
}

input UserInput {
  firstName: String
  lastName: String
}

type ResolveType {
  done: Boolean
}

type Query {
  getUser(id: String): User 
}

type Mutation {
  setUser(input: UserInput!): ResolveType!
}
`;

const resolvers = {
  Query: {
    getUser: (parent: any, {id}: any, context: any, info: any) => {
      console.log("id", id);
      return {
        firstName: "wooseok",
        lastName: "lee",
      };
    },
  },
  Mutation: {
    setUser: (parent: any, {firstName, lastName}: any, context: any, info: any) => {
      console.log("input:", firstName, lastName);
      return {
        done: true,
      };
    },
  },
};

app.use(applyGraphQL({
  typeDefs: types,
  resolvers: resolvers
}));

console.log("Server at http://localhost:8080");
await app.listen({ port: 8080 });

TODO

  • Add cache
  • Enable the upload
  • Enable the JSON scalar

Method

gql

GraphQL-tag Parsing GraphQL queries

  • gql A JavaScript template literal tag that parses GraphQL query strings into the standard GraphQL AST.

applyGraphQL

A Generator which based Attain Router class creates some middlewares for supporting the GraphQL.

Options

  • path?: string
    A target path that handles the GraphQL post request (*optional: default as /graphql)
  • typeDefs: any
    generated type tags by the gql
  • resolvers: any
    An object that handles the queries and mutations
const resolvers = {
  Query: {
    getUser: (parent: any, {id}: any, context: any, info: any) => {
      // ...query handling function here
    },
  },
  Mutation: {
    addUser: (parent: any, {firstName, lastName}: any, context: any, info: any) => {
      // ...add user codes here
    },
  }
}

The resolvers will be received these four parameters

  • parents: The return value of the resolver for this field's parent
  • args: An object that contains all GraphQL arguments provided for this field.
  • context: An object shared across all resolvers that are executing for a particular operation.
  • info: Contains information about the operation's execution state, including the field name, the path to the field from the root, and more.
  • context?: (req: Request) => any
    Send any objects to each resolver (*optional)
  • middlewares?: CallBackType[];
    (req, res) => any add array of middlewares into before execute the GQL post request (*optional)
  • usePlayground?: boolean;
    enable the playground at get method (*optional: default as true)

About

A simple GraphQL middleware for the Attain deno

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published