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

Automatic SDK generation for GraphQL API #12

Open
egoist opened this issue Jan 16, 2020 · 5 comments
Open

Automatic SDK generation for GraphQL API #12

egoist opened this issue Jan 16, 2020 · 5 comments
Labels
open-source-library Feel free to work on it unless someone else has claimed it

Comments

@egoist
Copy link
Owner

egoist commented Jan 16, 2020

The problem

When accessing a GraphQL API, you typically do this:

apolloClient.mutate(gql`
  mutation MyMutation($name: String!, $password: $String!) {
    insert_user(objects: [{name: $name, password: $password}]) {
      returning {
        id
      }
    }
  }`,
  variables: {
    name: 'kevin',
    password: 'xxx'
  }
)

It's verbose, but if you also use TypeScript, a decent amount of manual type declarations are needed for type safety and that makes it even more verbose:

type OperationResult = {
  insert_user: {
    returning: Array<{ id: string }>
  }
}

type OperationVariables = {
  name: string
  password: string
}

apolloClient.mutate<OperationResult, OperationVariables>(gql`
  mutation MyMutation($name: String!, $password: $String!) {
    insert_user(objects: [{name: $name, password: $password}]) {
      returning {
        id
      }
    }
  }`,
  variables: {
    name: 'kevin',
    password: 'xxx'
  }
)

The solution

I wonder if it's possible/practical to generate a JS/TS/Go SDK with built-in types from a random GraphQL endpoint, given that you are able to access its schema file.

So instead of using DSL, you use an actual language instead:

const client = require('./generated-client')

await client.insert_user({
  objects: [{ name: 'kevin', password: 'xxx' }],
  select: {
    returning: {
      id: true
    }
  }
})

And it's type-safe by default.

@egoist egoist added the open-source-library Feel free to work on it unless someone else has claimed it label Jan 16, 2020
@uetchy
Copy link

uetchy commented Jan 16, 2020

I like this idea. I’ve made something like that but for REST API. For REST API there’s no way to obtain API scheme so I failed to make it really convenient.
https://github.com/uetchy/MuffledAPI

GraphQL would play nice with API client generator.

@egoist
Copy link
Owner Author

egoist commented Jan 20, 2020

https://github.com/graphql-editor/graphql-zeus is exactly what I was talking about 😮

@egoist
Copy link
Owner Author

egoist commented Jan 20, 2020

Here's a more low-level library, only for generating typescript typings: https://graphql-code-generator.com/docs/getting-started/

@egoist
Copy link
Owner Author

egoist commented Jan 22, 2020

Turns out graphql-code-generator can also generate a full client, you just need to combine multiple plugins: https://github.com/graphile/starter/blob/master/%40app/graphql/codegen.yml

@egoist
Copy link
Owner Author

egoist commented Feb 5, 2020

A list of solutions: https://twitter.com/HasuraHQ/status/1222197037459890176

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source-library Feel free to work on it unless someone else has claimed it
Projects
None yet
Development

No branches or pull requests

2 participants