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

Use apollo-codegen itself as webpack loader #360

Closed
infogulch opened this issue Jan 16, 2018 · 3 comments
Closed

Use apollo-codegen itself as webpack loader #360

infogulch opened this issue Jan 16, 2018 · 3 comments

Comments

@infogulch
Copy link

infogulch commented Jan 16, 2018

Once apollo-codegen generated source produces a fully-typesafe callable function directly from a .graphql source file (a la #308), what I'd love to see is to convert/wrap apollo-codegen into a webpack-style loader where you load a .graphql file, and it exports the mentioned typesafe functions directly.

The idea is to add the loader, somehow point it to an existing schema.json file, and when you import from a .graphql/.gql file it automatically generates everything you need without any generation step. Like a higher-level graphql-tag loader.

This is is the experience I would hope for:

[myquery.graphql]

query FetchRestaurants($menuItemId: String!) {
  fetchMenuItem(menuItemId: $menuItemId) {
    id
    categoryId
  }
}

[mycode.ts]

import { FetchResturants } from './myquery.graphql';
// no running `apollo-codegen generate` , creating or checking in additional files, etc.

function doStuff(client: GraphQLClient) {
  // ok
  FetchResturants(client, "1a3fd").subscribe((result) => {
    console.log(result.data.fetchMenuItem[0].categoryId);
  });

  // type error: FetchResturants takes a string not an integer
  FetchResturants(client, 123).subscribe((result) => {
    // type error: fetchMenuItem has no property named `category` (it's `categoryId`)
    console.log(result.data.fetchMenuItem[0].category);
  });
}

These libraries are so close to a completely typesafe but also trivial to use api surface all the way from the server through client libraries into UI components via GraphQL I can taste it.

@bgw
Copy link

bgw commented Jan 17, 2018

For this to work properly, you'd need support for type providers.

Typescript has an open issue for this: microsoft/TypeScript#3136
And Flow does too: facebook/flow#722

@infogulch
Copy link
Author

infogulch commented Jan 28, 2018

Ah I see. Thanks for the links.

It looks like neither of those are moving very quickly, unfortunately.

I'd still love this, but 308 looks more immediately attainable. :)

@maxpain
Copy link

maxpain commented Jun 15, 2021

any news?

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