kamilkisiela / apollo-angular

A fully-featured, production ready caching GraphQL client for Angular and every GraphQL server 🎁
https://apollo-angular.com
MIT License
1.5k stars 309 forks source link

flag incorrect query and mutations on compilation #1766

Closed hexdecimal16 closed 1 week ago

hexdecimal16 commented 2 years ago

How can I flag incorrect queries and mutations during compilation?

Since query and mutation are essentially strings how safe are they to use, because and change in schema can't be refected clearly on those queries.

kamilkisiela commented 2 years ago

What do you mean by "flag" and could you elaborate a bit more and even share some examples?

hexdecimal16 commented 2 years ago

Example: schema

type User {
  id: String!
  username: String!
  balance: Float!
  amountAdded: Float!
}
type Query {
 """Returns all users"""
  user(): [User]
}

query

const GET_USERS = gql`query User {
  user {
    username
  }
}`

Now suppose I rename username to name on my backend. How will frontend know that this query is wrong now? There should be some flag that can warn or throw error while building/compilation.

Ideally, there should be an option to pass my schema that can be used to resolve query/mutations while compilation. This will help to catch errors ahead of time. This option could be enabled on development.

I use apollo-server-express for my backend and they have an option to automatically generate schema whenever server gets started. @kamilkisiela

kamilkisiela commented 2 years ago

There are tools for that, like GraphQL Inspector to validate documents against the schema or GraphQL Code Generator to generate TypeScript types and interfaces. https://www.the-guild.dev/blog/graphql-with-typescript-done-right

https://the-guild.dev/blog/new-graphql-inspector

hexdecimal16 commented 2 years ago

There are tools for that, like GraphQL Inspector to validate documents against the schema or GraphQL Code Generator to generate TypeScript types and interfaces. https://www.the-guild.dev/blog/graphql-with-typescript-done-right

https://the-guild.dev/blog/new-graphql-inspector

I think it will be more beneficial if we have it in the client instead of adding more 3rd party dependency in the project

PowerKiKi commented 1 week ago

The modern solution is to use https://the-guild.dev/graphql/codegen

hexdecimal16 commented 1 week ago

Thanks @PowerKiKi will look into this