Skip to main content
GraphQL

Create Mutation

QueryParseValidateEasy

What you will learn

  • Mutations are not idempotent by default โ€” clients must handle retry-after-success carefully
  • Input types, not scalar arguments, keep the API evolvable โ€” assert input object shape
  • Return shape should include the created object AND any derived fields (id, createdAt)

Description

Create a new resource using GraphQL mutation.

Test Scenario

const mutation = `
  mutation CreateUser($input: CreateUserInput!) {
    createUser(input: $input) {
      id
      name
      email
    }
  }
`;
const variables = {
  input: { name: "Test", email: "test@test.com" }
};
Endpoint/api/graphql

Testing Tips

  • โ€ขMutations modify data
  • โ€ขReturn created object fields
  • โ€ขUse input types for complex data
// Playwright - GraphQL Mutation
import { test, expect } from '@playwright/test';
test('GraphQL mutation to create user', async ({ request }) => {
const response = await request.post('/api/graphql', {
data: {
query: `
mutation CreateUser($input: CreateUserInput!) {
createUser(input: $input) {
id
name
email
}
}
`,
variables: {
input: {
name: 'New User',
email: 'new@example.com'
}
}
}
});
expect(response.ok()).toBeTruthy();
const { data } = await response.json();
expect(data.createUser.id).toBeDefined();
expect(data.createUser.name).toBe('New User');
});

Challenge ID: graphql-mutation-create

2 frameworks available

API Playground

Send requests and inspect responses

Endpoint: /api/graphql

Press Enter to send