Search code examples
graphqlapolloreact-apollo

Parameterized queries qraphQL


I want to make parameterized requests from the Apollo client to the Apollo server.

On client:

const GET_VALUES = gql`
query Values($desc: String!) {      
    Values      
}    
`;
function ValueSelector({ pickValue, dirDesc }) { 
const { loading, data, error } = useQuery(GET_VALUES, {
variables: { dirDesc },
 });
}

On server (schema):

type Query {
Values(desc: String!): [String]
@cypher(
  statement: "MATCH (:Dir{description:$desc})-[:value]->(v) RETURN collect(v.TXTLG)"
 )  
}

Result:

[GraphQL error]: Message: Field "Values" argument "desc" of type "String!" is required, but it was not provided., Location: [object Object], Path: undefined
[Network error]: ServerError: Response not successful: Received status code 400

Solution

  • const GET_VALUES = gql`
        query Values($desc: String!) {      
          Values(desc: $desc)  
        }    
      `;
    
    function ValueSelector({ pickValue, dirDesc }) {   
      const { loading, data, error } = useQuery(GET_VALUES, {
        variables: { desc:dirDesc},
       });