graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

Graphql does not replace variables into gql query

Open canali83 opened this issue 4 years ago • 3 comments

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 );

canali83 avatar Aug 26 '20 17:08 canali83

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 avatar Aug 27 '20 07:08 Sceat

@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 avatar Jan 15 '21 09:01 Karl-EdwardFPJeanMehu

@Karl-EdwardFPJeanMehu both are optionals

Sceat avatar Jan 15 '21 10:01 Sceat