Comments (24)
If you guys are interested, I'm also working on a converter that approaches this from the opposite direction: https://github.com/convoyinc/ts2gql
from apollo-server.
also i came across http://graphql-codegen.com which works very good in my experiance, used it to build typings for schema in compile type, while resolvers are using this typings so if scheme has breaking change, it will pop right up on compilation :)
from apollo-server.
Apollo Codegen now supports TypeScript and soon Flow: https://github.com/apollostack/apollo-codegen
Note that if you are using this for client-side type generation the best way is to generate specific types for every query, so that you can ensure your queries have loaded all of the fields you need for your UI.
from apollo-server.
@martijnwalraven is working on a code generator that might be able to address this use case!
from apollo-server.
Here's an incredible rudimentary script we run. It will convert our schema file to typescript defs.
https://gist.github.com/vangorra/cd75c27805aafd3c070fdcc8c3bc496b
from apollo-server.
Yeah, I think that's a great idea, more something for the client though. You have to be careful to create typescript definitions for query results, and not definitions for the whole schema, because GraphQL results don't actually have all the fields that the schema has!
Something to keep in mind if you want to take a stab at it. If you give it a try, let us know how it goes.
from apollo-server.
@bkinsey808 Are you still interested in pursuing this?
from apollo-server.
to be honest I'm not really sure how to proceed.
from apollo-server.
I don't actually know either, but for a start, you could probably ask the person who wrote gql2ts how you might go about something like that. If you're using it on the client, you need to create types based on the query (given the schema, of course), which is different from what he is doing now. I don't know if it's possible to build that into the compiler, but even if you had to generate the types with an automatic build script before you can use them, that might already be useful.
from apollo-server.
On further investigation, it seems that overloads could be relevant to this question, because what you want to do is have a different return type based on which query you passed to the watchQuery
or query
function in Apollo Client, or runQuery
on Apollo Server (after the refactoring I'm currently doing). Does that make sense?
from apollo-server.
@helfer yes, that makes sense. But for overloads to work, if I understand correctly, the input parameter for query or watchQuery would have to be specifically typed.
from apollo-server.
Yeah I think you would basically need to code generate a type for each query and fragment.
from apollo-server.
Ideally, I'd like it if interfaces could be generated from the schema at point of compile, named in a standard way, and the client could import them in a standard way.
from apollo-server.
The issue with generating the types from the schema directly is that GraphQL allows you to ask for only a subset of the fields on that type. So it might look like your code is type-checked, but it's actually not. That's why types have to be generated based on fragments or queries.
from apollo-server.
Having to generate types per query seems like a real drag. Is it even possible if the queries are generated dynamically? Seems like a perfect use case for nullable types, no? If I understand correctly, by using nullable types, it doesn't matter what subset of fields are chosen from the schema.
from apollo-server.
@bkinsey808 You can't have type checking at compile time and dynamic queries. You have to pick one or the other.
Nullable means the field is null. That's different from it not being there at all, which is what GraphQL queries are all about.
Since this issue isn't really related to apollo-server, but graphql-js in general, I feel like maybe you should open the issue on that repo. That would also make it more likely that other people who are interested in it will find it.
from apollo-server.
Ok, not nullable, how about Undefineable?
from apollo-server.
That's quite interesting! Curious how you will end up using that.
from apollo-server.
We've got a pretty hefty hierarchy of typescript definitions for our various resources already; it's working quite well so far to consume those interfaces (and a new one that describes the GraphQL root query).
The output is then piped into apollo-server:
export default buildSchemaFromTypeDefinitions([
fs.readFileSync('path/to/schema.gql', {encoding: 'UTF-8'}),
`
schema {
query: RootQuery
}
`,
// And eventually any other special cases we may want to include
]);
I suppose I could have gone straight to the underlying GraphQL object model, but this is certainly more inspectable :P
from apollo-server.
Sounds good to me! Pretty exciting :] I'd love to see a code sample or blog post about this at some point!
from apollo-server.
Closing this issue for now, since it's been inactive for a while.
from apollo-server.
Typescript 2.1's keyof
operator comes in to the rescue.
Very interesting feature that could enable creating sub-types of a bigger schema type without preprocessing the code. See:
https://blogs.msdn.microsoft.com/typescript/2016/12/07/announcing-typescript-2-1/
from apollo-server.
http://graphql-codegen.com/ no longer exists
from apollo-server.
I wrote https://www.npmjs.com/package/typescript-typedefs for this purpose.
from apollo-server.
Related Issues (20)
- support for fastify version 4 HOT 2
- Allow passing http2 server to ApolloServerPluginDrainHttpServer
- ApolloServerPluginUsageReporting reportTimer has incorrect type HOT 1
- Elasticache cluster - CROSSLOT Keys in request don't hash to the same slot HOT 4
- Unable to package/deploy with serverless using Node14 and apollo-serverless-core HOT 3
- ApolloServerPluginUsageReporting does not allow to pass http agent HOT 11
- Misconfigured `sendVariableValues` results in similar behavior as `{all: true}` HOT 3
- ApolloServerPluginUsageReporting does not allow to pass http agent HOT 2
- Apis that have their requests cached dont need to make unnecessary auth token fetches
- Document known mitigated security issue in `@koa/cors` for AS3 users HOT 1
- Update `@koa/cors` dependency for AS3 (or why we don't intend to) HOT 2
- Share and modify variables between Express and the GraphQL/Apollo server HOT 1
- Error: ApolloServer<BaseContext>' is not assignable to type 'ApolloServer<ExpressContext> HOT 3
- callback function for onSchemaLoadOrUpdate when upgrading from AS v3 to AS v4 HOT 3
- Allow plugins to throw meaningful GraphQL errors
- Getting started tutorial has typescript issues...
- Delay when subscription is closed on client side of the router when using callback
- Documentation and dynamic cache control feature of Apollo leverages incompatible GraphQl type interface. HOT 3
- Disabling introspection should also disable "Did you mean ...?" HOT 2
- Undefined 'code' TypeError within errorNormalize HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apollo-server.