oak-graphql icon indicating copy to clipboard operation
oak-graphql copied to clipboard

schema field in applyGraphQL

Open quenginedev opened this issue 5 years ago • 6 comments

Is there going to be a schema field for generated schema using "new GraphQLSchema(<GraphqlType>)"?

quenginedev avatar Jun 03 '20 21:06 quenginedev

No, I'm using the makeExecutableSchema for generating the schemas. This's part of graphql-tools

aaronwlee avatar Jun 03 '20 22:06 aaronwlee

I'll like to say you have done an awesome work. I'm a bit new to GraphQL and I wanted to know what if you already had a generated schema and you would want to use the schema?

{
  path = "/graphql",
  schema
  typeDefs,
  resolvers,
  ...
}

you could make a check if a schema field is passed as an argument like typeDefs and resolvers and skip the makeExecutableSchema, this will help those who already have their generated schemas use the endpoint.

if(!schema){
  schema = makeExecutableSchema({ typeDefs, resolvers });
}

const result = await (graphql as any)(
          schema,
          ...
        );

Thanks so much

quenginedev avatar Jun 04 '20 12:06 quenginedev

sorry I could not fully understand the generated schemas meaning. can you share with me a sample of the generated schemas and how you use it?

aaronwlee avatar Jun 04 '20 13:06 aaronwlee

const { graphql, GraphQLSchema, GraphQLObjectType, GraphQLString } = 'https://raw.githubusercontent.com/adelsz/graphql-deno/v15.0.0/mod.ts'

const schema = new GraphQLSchema({
  Query: new GraphQLObjectType({
    name: 'Query',
    fields: () => ({
      hello: {
        type: GraphQLString,
        resolve: () => 'Hello world!'
      }
    })
  })
});

graphql(schema, '{ hello }').then((response) => {
  console.log(response);
});

quenginedev avatar Jun 04 '20 13:06 quenginedev

I see it. This is the wrong behavior of using this oak-graphql. If you want to use already generated schemas and resolvers, you should not use this project. makeExecutableSchema is a validator and it's checking the types and resolvers types are matching for verifying the server will be working fine. And, this is the why I use the graphql-tag and graphql-tools library.

if you're going to use that generated schemes, just use exactly same as you given to me for starting the graphql server.

here is a code example when you apply it to the middleware.

app.use(async ctx => {
  if (ctx.request.hasBody) {
    const body = await ctx.request.body();
    const { query, variables, operationName } = body.value;

    const result = await graphql(
      schema,
      query,
      resolvers,
      undefined, // or send the context
      variables,
      operationName,
    );

    ctx.response.body = result;
  }
})

aaronwlee avatar Jun 04 '20 14:06 aaronwlee

Thanks very much, bro you are a lifesaver 😄.

quenginedev avatar Jun 04 '20 15:06 quenginedev