0
votes

I have a mutation like this -

import gql from "graphql-tag";

const completedAt = new Date().toISOString();

export const CREATE_TODO = gql`
  mutation($body: String!, $completedAt: DateTime = completedAt) {
    createTodo(input: { body: $body, completed_at: $completedAt }) {
      id
      body
    }
  }
`;

When I call this mutation I get the error

GraphQL error: Default value for $completedAt doesn't match type DateTime in React Apollo

How do I solve this? I have tried JSON.stringify(new Date().toISOString()) too but no luck :(

Also, note that this works -

import gql from "graphql-tag";

const completedAt = JSON.stringify(new Date().toISOString());

export const CREATE_TODO = gql`
  mutation($body: String!) {
    createTodo(input: { body: $body, completed_at: ${completedAt} }) {
      id
      body
    }
  }
`;
1

1 Answers

0
votes

I changed my solution. Right now, what I do is I don't use completedAt like that. I take it as an input to GraphQL variable like:

import gql from "graphql-tag";

export const CREATE_TODO = gql`
  mutation createTodo($body: String!, $completedAt: DateTime) {
    createTodo(input: { body: $body, completed_at: $completedAt }) {
      id
      body
      completed_at
    }
  }
`;

And then, as $completedAt is not a required field, I send null if I don't want any value or I send new Date().toISOString() if I want it to have the current time.

For example:

Incomplete Todo, i.e, Null value for DateTime

mutate({
    query: CREATE_TODO,
    variables: { body: "Incomplete todo", completedAt: null }
});

Complete Todo, i.e, Current time for DateTime

mutate({
    query: CREATE_TODO,
    variables: { body: "Complete todo", completedAt: new Date().toISOString() }
});