remorses / genql

Type safe TypeScript client for any GraphQL API
https://genql.dev
MIT License
872 stars 37 forks source link

Assumption about graphQL errors returned #176

Closed Notalifeform closed 3 months ago

Notalifeform commented 3 months ago

CreateFetcher assumes that a graphQL error contains an errors root-node https://github.com/remorses/genql/blob/master/cli/src/runtime/fetcher.ts#L62

I'm not sure whether or not this defined in the graphql standard or not, but when querying Monday.com and I receive

{
  "error_message": "The board does not exist. Please check your board ID and try again",
  "error_code": "InvalidBoardIdException",
  "error_data": {
    "board_id": 22222222
  },
  "status_code": 200,
  "account_id": 3333333
}

so I created a fetcher wrapper (see below) to meet the assumptions of CreateFetcher, but I think it would be better to provide accept a method that will can determine if there was an error and extract them.

Would that be an option?

function createMondayFetcher2(url, headers = {}) {
  const myFetcher: Fetcher = createFetcher({ url, headers });
  return async (gql) => {
    const json = await myFetcher(gql);
    if (json.error_message) {
      return { errors: [json] };
    }
    return { data: json };
  };
}
linear[bot] commented 3 months ago

GEN-31 Assumption about graphQL errors returned

remorses commented 3 months ago

That API doesn’t follow the spec, overriding fetch is the best solution here.

Notalifeform commented 3 months ago

you're right - I just found the reference to the actual spec: https://spec.graphql.org/October2021/#sec-Errors