Right now I have this tag below. It's static and will always get a comment with the id of 3. Is there a possible way to put a variable inside this graphQL-tag. So I can re-use the graphQL-tag, and just change the variable ID?
export const GET_COMMENTS: any = gql`
{
comments(id: 3) {
userId,
text,
creationDate,
}
}
`;
Thanks in advance!
Use GraphQL variables for type safety and self-documenting queries. GraphQL variables offer an extra layer of protection in your queries, namely type safety — meaning that a query will only accept dynamic variables of certain data types, such as String, Int (number), DateTime and so on.
When you're passing arguments in code, it's generally better to avoid constructing the whole query string yourself. Instead, you can use $ syntax to define variables in your query, and pass the variables as a separate map. . then(data => console.
It uses React's render props pattern. import React from "react"; import { Query } from "react-apollo"; import gql from "graphql-tag"; const GET_TODOS = gql` { todos { id type } } `; const Todos = () => ( <Query query={GET_TODOS}> {({ loading, error, data }) => { if (loading) return <p>Loading...
Variables simplify GraphQL queries and mutations by letting you pass data separately. A GraphQL request can be split into two sections: one for the query or mutation, and another for variables. Variables can be declared after the query or mutation and are passed like arguments to a function and begin with $ .
Yeah, you can pass variables in the GQL queries. $xyz
is kind of notation or variable name to pass variables in GQL.
export const GET_COMMENTS: any = gql`
query GET_COMMENTS($id: Int){ // $id is the variable name
comments(id: $id) {
userId,
text,
creationDate,
}
}
`;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With