nicolasdao / graphql-s2s

Add GraphQL Schema support for type inheritance, generic typing, metadata decoration. Transpile the enriched GraphQL string schema into the standard string schema understood by graphql.js and the Apollo server client.
Other
187 stars 15 forks source link

Support for graphql-yoga Server #4

Closed geirman closed 6 years ago

geirman commented 6 years ago

It's not super popular (yet), but it is a graph.cool open source project, so I'd expect it to pick up steam. It basically bundles several packages together to give you a reasonable default setup, making it easy to get started with. But I had to monkey patch them in order to get your library to play nicely with them.

Then you'll have to monkey patch the graphql-yoga server in the following way so that graphql-s2s will work. NOTE: This wouldn't be necessary if I went with straight GraphQL-js.

File: https://github.com/graphcool/graphql-yoga/blob/master/src/index.ts#L13

// Import dependencies at the top
const graphqls2s = require('graphql-s2s');
const transpileSchema = graphqls2s.graphqls2s.transpileSchema;

Then wrap typeDefs with the transpileSchema fn https://github.com/graphcool/graphql-yoga/blob/master/src/index.ts#L48

this.schema = graphql_tools_1.makeExecutableSchema({
    typeDefs: [transpileSchema(typeDefs)],
    resolvers: __assign({}, uploadMixin, resolvers),
});

I tried transpiling typeDevs before sending it to yoga's GraphQLServer, but I got an error saying typeDefs must be an executableSchema.

NOTE: I had to import transpileSchema a bit differently than your example in the docs because const { transpileSchema } = graphqls2s did not destructure properly and gave me "transpileSchema is not a function" errors.

nicolasdao commented 6 years ago

Cool. I didn't know graphql-yoga so I'll definitely check that one out @geirman . I'll have a look at all this during the weekend. Thank you heaps for that awesome feedback. This is super useful.

glennvgastel commented 6 years ago

Here's how to use this package with the current version of graphql-yoga :

const { GraphQLServer } = require('graphql-yoga');
const { graphqls2s } = require('graphql-s2s');
const glue = require('schemaglue');

// Schemaglue is optional, of course
const { schema, resolver } = glue('src/graphql');

const server = new GraphQLServer({
  typeDefs: graphqls2s.transpileSchema(schema),
  resolvers: resolver,
});

const options = {
  port: process.env.PORT || 3000,
  endpoint: '/graphql',
  subscriptions: '/subscriptions',
  playground: '/playground',
}

server.start(options, ({ port }) =>
  console.log(
    `Graphql server running on port ${port}!`,
  ),
);

No further editing of the graphql-yoga package is therefore necessary anymore, so I'm closing this issue. Feel free to comment if you still run into issues.