Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql prepare should generate binding initializer #166

Closed
kbrandwijk opened this issue Jan 20, 2018 · 4 comments
Closed

graphql prepare should generate binding initializer #166

kbrandwijk opened this issue Jan 20, 2018 · 4 comments

Comments

@kbrandwijk
Copy link
Contributor

Currently, graphql prepare generates the static binding class, but it doesn't generate the initialization part of it (e.g. new Binding(...)). This means you have to manually duplicate the endpoint, headers and schema location in code.

To improve this workflow, the graphql prepare command could generate this for you.

Now the naive implementation would take whatever graphql-config comes up with, and use that. However, there are two catches here.

First, graphql-config will resolve a prisma endpoint through the prisma extension, so it will have an actual endpoint URL and authorization header. Code generation however, should use prisma-binding, not graphql-binding, because of additional features for Prisma.

Second, graphql-config resolves environment variables. However, if someone is using environment variables in their .graphqlconfig, it's highly likely that they would also want to use env vars in the code.

The implementation proposal (that will follow) should take these two points into account.

@kbrandwijk
Copy link
Contributor Author

kbrandwijk commented Jan 20, 2018

First, the basic use case without env vars:

So let's take the following .graphqlconfig configuration:

projects:
  app:
    schemaPath: src/schema.graphql
    extensions:
      endpoints:
        default: http://localhost:4000
  database:
    schemaPath: src/generated/prisma.graphql
    extensions:
      prisma: database/prisma.yml
      prepare-binding:
        output: src/generated/prisma.ts
        generator: prisma-ts
  bahnhof:
    schemaPath: src/generated/bahnhof.graphql
    extensions:
      endpoints:
        default:
          url: 'https://api.deutschebahn.com/free1bahnql/v1/graphql'
          headers:
            Authorization: Bearer myToken
      prepare-binding:
        output: src/generated/bahnhof.ts
        generator: binding-ts

To use these bindings in my code, I will need the the following for bahnhof (see also dotansimha/graphql-binding#30):

const link = new HttpLink({ uri, fetch })
  const middlewareLink = (operation, forward) => {
    console.log('operation', operation)
    if (authorizationToken) {
      operation.setContext(context => ({
        ...context,
        headers: {
          ...context.headers,
          Authorization: `Bearer ${authorizationToken}`
        }
      }))
    }
    return forward(operation)
  }

  const executableSchema = makeRemoteExecutableSchema({
    schema: fs.readFileSync('src/generated/bahnhof.graphql', 'utf-8'),
    link: ApolloLink.from([middlewareLink, link]),
  });

const binding = new Binding({ schema: executableSchema })

And the following for database:

const binding = new Prisma({
      endpoint: 'https://eu1.prisma.sh/public-patternraccoon-961/my-service/dev',
      secret: 'mysecret123', // specified in database/prisma.yml
      debug: true, // log all GraphQL queries & mutations
    }),

Now if graphql prepare could create a code file with init() methods, the server file would look like:

import { initDatabaseBinding, initBahnhofBinding } from 'src/generated/initBindings.ts'

const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers,
  context: req => ({
    ...req,
    db: initDatabaseBinding(),
    bahnhof: initBahnhofBinding()
  }),
})

@ardatan ardatan closed this as completed Oct 28, 2019
@Urigo Urigo reopened this Oct 29, 2019
@Urigo
Copy link
Owner

Urigo commented Oct 29, 2019

We've just released a new version and an alpha release channel for GraphQL CLI - 4.0.0-alpha.XXX!

In order to receive schemas the new new GraphQL Config which let's you specify multiple schemas from any source possible.

I want to ask the people on this issue, in that case, why would you need the prepare command?
Are we missing anything and should we add this command back?

Checkout new instructions and the migration guide and we will be happy to hear your feedback during the alpha period!

Feel free to contact us by opening an issue or using our Discord channel;
https://discord.gg/xud7bH9

@Urigo
Copy link
Owner

Urigo commented Oct 29, 2019

I've posted the wrong Discord link, this is the correct one: https://discord.gg/xud7bH9

@wtrocki
Copy link
Collaborator

wtrocki commented Sep 4, 2020

This command is no longer supported in 4.0.0. Closing

@wtrocki wtrocki closed this as completed Sep 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants