prismake / typegql

Create GraphQL schema with TypeScript classes.
https://prismake.github.io/typegql/
MIT License
424 stars 21 forks source link

Unable to merge multiple schemas generated by compileSchema using mergeSchemas function from graphql-tools #19

Closed shani117 closed 6 years ago

shani117 commented 6 years ago

I was trying to merge 2 different GraphQLSchema created by compiling 2 different typegql schemas using the standard mergeSchemas function from the graphql-tools. After that, when I try to start the server and hit the /graphql endpoint, I get an error saying:

{"errors":[{"message":"GraphQL middleware options must contain a schema."}]}

Is this something that should work as I expect it would? I just took the schema in the "basic-express-server" example and the schema in the "nested-mutation-or-query" example and tried to merge them like so:

import { schema, schemaAdv } from './schemas';

const compiledSchema = mergeSchemas({schemas: [schema, schemaAdv]});

const app = express();

app.use(
  '/graphql',
  graphqlHTTP({
    compiledSchema,
    graphiql: true,
  }),
);
app.listen(3000, () => {
  console.log('Api ready on port 3000');
});

Even though the server starts up just fine, trying to navigate to the /graphql endpoint throws the above error.

If this is not the supported way to merge schemas, is there any other way? Any pointers?

Thanks, Shani.

pie6k commented 6 years ago

I've created example of merging schemas with graphql-tools here and it works perfectly fine.

Issue might be that both basic-express-server and nested-mutation-or-query examples have hello field at root level and it might cause a conflict.

Could you send me more details about your case, or could you try to "break" example I've created and let me know how you did it so I can reproduce your issue?

shani117 commented 6 years ago

This is really weird. I think I'm missing something maybe. So I took your nested-mutation-or-query example and ran it, works perfectly. Then I just renamed a few things and suddenly I start seeing this error even on the nested-mutation-or-query example!!

This is the change I did on the base example:

@Schema()
class MyAdvSchema {
  @Mutation()
  book(bookId: number): BookMutation {
    return new BookMutation(bookId);
  }

  @Query()
  hello(): string {
    return 'World!';
  }
}

export const schema = compileSchema(MyAdvSchema);

Note the change of the Schema class name from MySchemato MyAdvSchema.

I even tried to just rename the const export to schemaAdvand leave the schema class name untouched, same error. Nothing else is changed in the example code. I can't seem to put my finger on what could be causing this behavior. Do you have any thoughts on that?

I'll try your other example you created and see if I can repro something with that example.

Thanks.

shani117 commented 6 years ago

Accept my apologies on this one - it was a silly oversight on my part. When supplying the schema to graphqlHTTP, I was not providing the prop name for schema and just passing in the compiled schema directly and that was what was causing the issue. Once I supplied the prop name, and then the compiled/merged schema, everything works correctly.

Once again, sorry for the confusion.

Thanks, Shani.