Apollo-client: Error: GraphQL error: field "company_id" not found in type: 'teams_insert_input'

Created on 21 Jul 2019  路  9Comments  路  Source: apollographql/apollo-client

The query is:

export const ADD_TEAM_QUERY = gql`
  mutation addTeam($name: String!, $description: String, $companyId: uuid!) {
    insert_teams(
      objects: {
        name: $name
        description: $description
        company_id: $companyId
      }
    ) {
      returning {
        id
        name
        description
        created_at
        company_id
      }
    }
  }
`

The error is:

Error: GraphQL error: field "company_id" not found in type: 'teams_insert_input'
at new ApolloError (bundle.esm.js:76)
at Object.next (bundle.esm.js:1274)
at notifySubscription (Observable.js:152)
at onNotify (Observable.js:196)
at SubscriptionObserver.next (Observable.js:248)
at bundle.esm.js:1079
at Set.forEach ()
at Object.next (bundle.esm.js:1078)
at notifySubscription (Observable.js:152)
at onNotify (Observable.js:196)
at SubscriptionObserver.next (Observable.js:248)
at bundle.esm.js:107

But when I use my back-end built-in graphql engine sends the request with the same query, everything works:

image

Script to generate Typescript

  • "schema": "apollo client:download-schema ./src/graphql-schema.json",
  • "types": "apollo client:codegen --includes=./src/queries/*.ts --addTypename --target=typescript"

Generated type:

// ====================================================
// GraphQL mutation operation: addTeam
// ====================================================

export interface addTeam_insert_teams_returning {
  __typename: "teams";
  id: any;
  name: string;
  description: string | null;
  created_at: any;
  company_id: any;
}

export interface addTeam_insert_teams {
  __typename: "teams_mutation_response";
  /**
   * data of the affected rows by the mutation
   */
  returning: addTeam_insert_teams_returning[];
}

export interface addTeam {
  /**
   * insert data into the table: "teams"
   */
  insert_teams: addTeam_insert_teams | null;
}

export interface addTeamVariables {
  name: string;
  description?: string | null;
  companyId: any;
}

Code to send the request:

         client
              .mutate({
                mutation: ADD_TEAM_QUERY,
                variables: {
                  name: values.name,
                  description: values.description,
                  companyId,
                },
                update: updateApolloCacheAfterMutation(GET_TEAMS_QUERY),
              })
              .then(() => {
                onClose()
              })
              .catch((e: Error) => {
                console.log(e)
              })

When did it happen:

Everything worked perfectly until I added this company_id column today to the database table and updated all the GraphQL schema locally. It stops working. I tried, clear the generated folder, and re-generate all the things, but still not work.

apollo.config.js

module.exports = {
  client: {
    service: {
      includes: ['src/queries/*.{ts,tsx,graphql}'],
      name: 'Dev',
      url: 'path-to-my-graphql-endpoint',
      headers: {
        // my headers
      },
    },
  },
}

Most helpful comment

@cortopy Wow, anyone who has trouble on this. xD
Yes, I am using Hasura.

The problem is, after updating that Permission (adding a new role) from Hasura GUI, somehow, the Hasura engine will not regenerate the graphQL schema on the serverside, which means, when you call it with the token of the role that you just added, it won't be there.

Even though you can use it in the Hasura Admin GUI. But you propertyly use the x-hasura-secret way. So, you won't have this problem, if you use that token, you wil see the same error.

How I solve it:

I ended up removing the role, and added it again... and after that, it works...

@shahidhk

All 9 comments

Turns out it is a server issue...

@Albert-Gao what was your issue and how did you solve it? I'm experiencing same problem and I don't know why it works in graphiql. Are you using hasura?

@cortopy Wow, anyone who has trouble on this. xD
Yes, I am using Hasura.

The problem is, after updating that Permission (adding a new role) from Hasura GUI, somehow, the Hasura engine will not regenerate the graphQL schema on the serverside, which means, when you call it with the token of the role that you just added, it won't be there.

Even though you can use it in the Hasura Admin GUI. But you propertyly use the x-hasura-secret way. So, you won't have this problem, if you use that token, you wil see the same error.

How I solve it:

I ended up removing the role, and added it again... and after that, it works...

@shahidhk

That was it!!! Thank you so much. I forgot I was using the anonymous user 馃憤

Thanks - Also a Hasura user here, and I realized my issue is that i just needed to add the column select permission on a new field I'd recently added.

I just had a similar issue that was permissions related. I made the mistake of thinking that any permission given to the anonymous/public role will automatically apply to all other roles. Turns out that is not the case, each role has to have their permissions set explicitly.

I'm also on Hasura, using version v1.2.2 and got this error. Unlike others, theres nothing wrong with my permissions, and even restarting the server locally on Docker did nothing to fix this.

Edit:

Was caused because my setup had 2 hasura servers running at the same time, but I didn't set the proper api-port for the second server, so the 2 servers had been running with the same api-port. This caused issues where my client was querying data on localhost:8080 but in reality I had the server on that shut down and my actual server I thought I was querying to was actually on localhost:8090. Somehow even though there was no server on localhost:8080, it was somehow querying data from that, and there were some weird inconsistencies and behaviour.

I just had to reset my docker instances wiping out their memory to get a clean state, then recreated the containers and applied the schema and the metadata to restore the server state. Also my client is now querying data on the proper endpoint.

https://github.com/hasura/graphql-engine/issues/5097

Hope this may help anyone else in a similar situation

Edit 2:

Got this error yet again. My old answer didn't work since there wasn't any changes to my database, but rather I had called a new and different mutation in place of an old one. This time I fixed it by fixing my query, which didn't have the return object structured properly (was caused because the syntax checking for my graphql queryy didn't work properly and I didn't copy and paste the query properly).

Thanks - Also a Hasura user here, and I realized my issue is that i just needed to add the column select permission on a new field I'd recently added.

Yes, I just had the same forehead slapping moment - after troubleshooting Apollo caching for 2 hours. If you're using a "collaboration token" or admin secret on the GraphiQL client it'll work fine from there.

I had a column preset in my permissions
Screen Shot 2020-11-11 at 11 38 07 PM

Was this page helpful?
0 / 5 - 0 ratings