graphql / graphql-js

A reference implementation of GraphQL for JavaScript
http://graphql.org/graphql-js/
MIT License
20.05k stars 2.02k forks source link

"Unknown type" error when running a known-working query, failing validation #3788

Closed jcollum-nutrien closed 4 days ago

jcollum-nutrien commented 1 year ago

Version:

  "peerDependencies": {
    "@apollo/client": "3.x",

Code:

const usePrescriptionListQuery = (
  input: PrescriptionListQueryInput,
  skip?: boolean
): QueryResult<RecommendationListResult> => {
  if (!input.branchId || input.branchId === 'skip') {
    console.warn('no branch ID supplied');
  }
  const { dataProductClient } = useDataProductClient();

  const query = useQuery(RECOMMENDATION_LIST_QUERY, {
    skip,
    variables: { input: input },
    client: dataProductClient,
  });

  return query;
};

RECOMMENDATION_LIST_QUERY is the query below.

Request payload from Chrome network panel:

{"operationName":"RecommendationList","variables":{"input":{"branchId":"5547","growerId":"b31327bc-fbec-4c31-95e5-67cd815eda92"}},"query":"query RecommendationList($input: RecommendationListQueryInput!) {\n  recommendationList(input: $input) {\n    nodes {\n      name\n      createdDate\n      type\n      recType\n      recId\n      fieldId\n      orderStatus\n      targetPests {\n        name\n        __typename\n      }\n      totalArea {\n        unit\n        value\n        __typename\n      }\n      products {\n        type\n        totalAmount {\n          unit\n          value\n          __typename\n        }\n        recProductId\n        flatRate {\n          unit\n          value\n          __typename\n        }\n        __typename\n      }\n      __typename\n    }\n    __typename\n  }\n}\n"}

Which is this with the \n turned into actual returns:

query RecommendationList($input: RecommendationListQueryInput!) {
  recommendationList(input: $input) {
    nodes {
      name
      createdDate
      type
      recType
      recId
      fieldId
      orderStatus
      targetPests {
        name
        __typename
      }
      totalArea {
        unit
        value
        __typename
      }
      products {
        type
        totalAmount {
          unit
          value
          __typename
        }
        recProductId
        flatRate {
          unit
          value
          __typename
        }
        __typename
      }
      __typename
    }
    __typename
  }
}

Actual result via web app (React):

{
    "errors": [
        {
            "message": "Unknown type \"RecommendationListQueryInput\".",
            "locations": [
                {
                    "line": 1,
                    "column": 34
                }
            ],
            "extensions": {
                "code": "GRAPHQL_VALIDATION_FAILED",
                "exception": {
                    "stacktrace": [
                        "GraphQLError: Unknown type \"RecommendationListQueryInput\".",
                        "    at Object.NamedType (/usr/src/app/node_modules/graphql/validation/rules/KnownTypeNamesRule.js:57:29)",
                        "    at Object.enter (/usr/src/app/node_modules/graphql/language/visitor.js:323:29)",
                        "    at Object.enter (/usr/src/app/node_modules/graphql/utilities/TypeInfo.js:370:25)",
                        "    at visit (/usr/src/app/node_modules/graphql/language/visitor.js:243:26)",
                        "    at validate (/usr/src/app/node_modules/graphql/validation/validate.js:69:24)",
                        "    at validate (/usr/src/app/node_modules/apollo-server-express/node_modules/apollo-server-core/dist/requestPipeline.js:186:39)",
                        "    at processGraphQLRequest (/usr/src/app/node_modules/apollo-server-express/node_modules/apollo-server-core/dist/requestPipeline.js:98:34)",
                        "    at async processHTTPRequest (/usr/src/app/node_modules/apollo-server-express/node_modules/apollo-server-core/dist/runHttpQuery.js:187:30)"
                    ]
                }
            }
        },
        {
            "message": "Cannot query field \"recommendationList\" on type \"Query\".",
            "locations": [
                {
                    "line": 2,
                    "column": 3
                }
            ],
            "extensions": {
                "code": "GRAPHQL_VALIDATION_FAILED",
                "exception": {
                    "stacktrace": [
                        "GraphQLError: Cannot query field \"recommendationList\" on type \"Query\".",
                        "    at Object.Field (/usr/src/app/node_modules/graphql/validation/rules/FieldsOnCorrectTypeRule.js:48:31)",
                        "    at Object.enter (/usr/src/app/node_modules/graphql/language/visitor.js:323:29)",
                        "    at Object.enter (/usr/src/app/node_modules/graphql/utilities/TypeInfo.js:370:25)",
                        "    at visit (/usr/src/app/node_modules/graphql/language/visitor.js:243:26)",
                        "    at validate (/usr/src/app/node_modules/graphql/validation/validate.js:69:24)",
                        "    at validate (/usr/src/app/node_modules/apollo-server-express/node_modules/apollo-server-core/dist/requestPipeline.js:186:39)",
                        "    at processGraphQLRequest (/usr/src/app/node_modules/apollo-server-express/node_modules/apollo-server-core/dist/requestPipeline.js:98:34)",
                        "    at async processHTTPRequest (/usr/src/app/node_modules/apollo-server-express/node_modules/apollo-server-core/dist/runHttpQuery.js:187:30)"
                    ]
                }
            }
        }
    ]
}

But when I run the same query in the Apollo interface I get valid results.

image

Kinda stuck here. This seems like a bug since the identical query works in the Apollo web interface. We have a ton of similar queries that aren't running into these validation issues.

One thing: that validation error is incredibly unhelpful. What field didn't validate? Is a field missing? Line and column numbers are unhelpful, they usually don't resolve to anything meaningful in web app land.

jcollum-nutrien commented 1 year ago

I captured the request payload from studio.apollographql.com vs the request payload from the React code. The only difference was one carriage return at the end of the payload's query field.

image

image

yaacovCR commented 4 days ago

Closing this old issue, we need a graphql-js minimal reproduction to confirm a bug. Please feel free to reopen if you can provide more information!