0

I don't believe its a big deal, but my GraphIQL generates all of its queries and mutations from my PSQL schema. Is it possible to have it general a file for me with each change that I can use directly in my project? As currently I am having to write manually my queries/mutations as below and often I am changing the schema and then having to update all my instances of this.

export const UPDATE = gql`
  mutation updateOrganiserByOrganiserId(
    $organiserName: String!
    $address: String
    $address2: String
    $city: String
    $country: String
    $postCode: String
    $manufacturerId: Int
    $organiserId: Int!
    $competitionSystemId: Int!
  ) {
    updateOrganiserByOrganiserId(
      input: {
        clientMutationId: "updateOrganisation"
        organiserId: $organiserId
        organiserPatch: {
          organiserName: $organiserName
          address: $address
          address2: $address2
          city: $city
          country: $country
          postCode: $postCode
          manufacturerId: $manufacturerId
          competitionSystemId: $competitionSystemId
        }
      }
    ) {
      clientMutationId
    }
  }
`
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291

1 Answers1

1

No, there is no way to do that.

often I am changing the schema

That's your actual problem. Your schema should be immutable, and only ever be extended but never changed. From the GraphQL best practices on versioning: "new capabilities can be added via new types and new fields on those types without creating a breaking change. This has led to a common practice of always avoiding breaking changes and serving a versionless API."

So even when your Postgres database schema changes, keep your GraphQL schema the same (or add new fields to it) so that your mutations simply keep working. Postgraphile provides a great lot of tools (renaming of fields, deprecation annotations, computed fields, …) to support that.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I completely agree with this concept. The only cavat I would bring to your attention is that I am in extremely early alpha, so changes imo right now are perfectly valid. I wonder how the queries and mutations are generated on the fly for graphIQL still though. Thank you for the answer :) – Jamie Hutber Dec 25 '20 at 22:32
  • Queries and mutations are not generated at all, you consciously select the fields and arguments from the schema (which is generated by Postgraphile from your database schema). Of course the GraphiQL interface makes this easy, but you still need to choose yourself. – Bergi Dec 25 '20 at 22:36
  • And of course if you are still in an alpha stage you may throw parts of your schema out of the window and redo it in a completely different way, but that means the code that is making queries and mutations also needs to change considerably anyway. – Bergi Dec 25 '20 at 22:37
  • 1
    Interesting, yes I can see what you mean that it just an interface to let you build queries. It makes me want to jump to amplify, but really. I need the power of psql and not dynomodb :D Thank you again – Jamie Hutber Dec 25 '20 at 23:41
  • 1
    You should only pass one variable: $input, which should be the same type as the input argument to the mutation. This way your mutation document will not need to be updated when fields are added/changed. Combine this with TypeScript generated by GraphQL-code-generator and your system will let you know where your code needs updating to accommodate the changes. – Benjie Dec 26 '20 at 10:08
  • ha, 1 year later and I'm google the same questions :D and I've still found no way to generate them, even though graphiql seems to! – Jamie Hutber Oct 26 '21 at 16:02
  • @JamieHutber Like Benjie said, you shouldn't need to generate a query document like the one in your question. Pass a single `$input: OrganiserInput!`, and you don't have to update it when you get new fields in the input type. – Bergi Oct 26 '21 at 16:54