Home > Blockchain >  How to resolve validation error when querying with graphql? (typescript)
How to resolve validation error when querying with graphql? (typescript)

Time:12-22

I am trying to query by passing in the name field but I get two different errors.

"Validation error of type MissingFieldArgument: 
  Missing field argument id @ 'getBlog'"
"Validation error of type UnknownArgument: 
  Unknown field argument name @ 'getBlog'"

I was able to successfully query it with the id field. Im new to graphql and im using this on my app that also uses aws amplify and aws appsync.

schema.graphql

type Blog @model {
  id: ID!
  name: String!
  posts: [Post] @connection(keyName: "byBlog", fields: ["id"])
}

queries.ts

// this is an auto generated file. This will be overwritten

export const getBlog = /* GraphQL */ `
  query GetBlog($name: String!) { //changed from ($id: ID!)
    getBlog(name: $name) { //changed from (id: $id)
      id
      name
      posts {
        items {
          id
          title
          blogID
          createdAt
          updatedAt
        }
        nextToken
      }
      createdAt
      updatedAt
    }
  }
`;

app.tsx

const getRecord = async () => {
        const result = await API.graphql(graphqlOperation(getBlog, {name: "testing"}))
        console.log(result) 
    }

I also tried pushing it to aws amplify push but it detected no changes. I didnt expect it to as i didnt change anything in the schema, only the queries. thanks in advance

CodePudding user response:

If you look at your GraphQL schema you should see the definition for the GetBlog query. It likely looks like:

query {
  GetBlog(id: ID!): Blog
}

That particular query can only be queried by id. Assuming you have control of the server you should also be able to define a GetBlogByName query:

query {
  GetBlogByName(name: String): Blog
}

But then it will be up to you to ensure that name is unique or deal with potentially >1 response.

  • Related