graphql-request
graphql-request copied to clipboard
Graphql does not replace variables into gql query
Graphql does not replace variables into gql query instead posting variables as separated as below. What am i missing?
{,…} query: "↵ mutation {↵ register(↵ data: {↵ firstName: $name↵ email: $email↵ password: $password↵ lastName: "foo"↵ }↵ ) {↵ firstName↵ }↵ }↵" variables: { email: '"x"',name: "xx",password: "x"}
My current code
const query = gqlmutation { register( data: { firstName: $name email: $email password: $password } ) { firstName } }
;
const variables = { email: '"x"', name: "xx", password: "x" };
const data = await new GraphQLClient("http://localhost:4000/graphql").request( query, variables );
This code should throw you an error as you forgot to initiate the parameters
const query = gql`
mutation ($name: String $email: String $password: String) {
register(data: {
firstName: $name
email: $email
password: $password
}) {
firstName
}
}`
@Sceat, you forgot the operation name in your request. The variables should also be separated by commas. The request should be:
const query = gql`
mutation register($firstName: String, $email: String, $password: String) {
register( data: {
firstName: $firstName
email: $email
password: $password
}) {
firstName
}`
}`
@Karl-EdwardFPJeanMehu both are optionals