graphql

Is there any tool that can auto-generate GraphQL fragments from GraphQL schemas?


Given a GraphQL entity like this:

type Action @entity(immutable: true) {
  id: String!
  subgraphId: BigInt!
  block: BigInt!
  chainId: BigInt!
  from: Bytes!
  hash: Bytes!
  timestamp: BigInt!
  category: ActionCategory!
  contract: Bytes!
  fee: BigInt
  addressA: Bytes
  addressB: Bytes
  amountA: BigInt
  amountB: BigInt
}

I would like to auto-generate a fragment like this:

fragment ActionFragment on Action {
  id
  addressA
  addressB
  amountA
  amountB
  block
  category
  chainId
  contract
  from
  hash
  subgraphId
  timestamp
}

I know it's not hard to copy-paste the entity and then change it to a fragment, but we have many entities in our schema, and I would prefer an automated solution (if it exists). Maybe there is a tool/npm package that can do this?


Solution

  • I very much doubt such a tool exists, it goes against the spirit of GraphQL: you should only query what you need. You need to do that deliberately, and manually; especially when handling nested fields in a graph of entities you need to decide on the cut-off point. You cannot just query everything the schema provides.

    If you really always want to query the entire Action (and force everyone to do that), make it a scalar Action instead of a type in your schema. I would not recommend that though as you loose the field typing.