1
votes

I have an error in my application. I use Next.js / ApolloClient / GraphQL and Prisma. The error concerns the GraphQL schema and it's use in Mutation:

Error : Reason: 'end_date' Field 'end_date' is not defined in the input type ArticleCreateInput

I tried to copy/paste the global schema on each file to have the good corresponding data, but it doesn't work.

My data Model (datamodel.prisma):

type Article {
  id: ID! @id
  title: String!
  description: String!
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt
  image: String
  maxUserNumber: Int!
  greatImage: String
  street: String!
  begin_date: DateTime!
  end_date: DateTime!
  price: Int!
  user: User!
}

My schema.graphql:

createArticle(
    title: String!
    description: String!
    image: String
    maxUserNumber: Int!
    greatImage: String
    street: String!
    begin_date: DateTime!
    end_date: DateTime!
    price: Int!
  ): Article!

In my Genertated file (prisma.graphql):

input ArticleCreateInput {
  id: ID
  title: String!
  description: String!
  image: String
  maxUserNumber: Int
  greatImage: String
  street: String
  begin_date: DateTime
  end_date: DateTime
  price: Int!
  user: UserCreateOneInput!
}

ClientSide Mutation and State:

const CREATE_ARTICLE_MUTATION = gql`
  mutation CREATE_ARTICLE_MUTATION(
    $title: String!
    $description: String!
    $image: String
    $maxUserNumber: Int!
    $greatImage: String
    $street: String!
    $begin_date: DateTime!
    $end_date: DateTime!
    $price: Int!
  ) {
    createArticle(
      title: $title
      description: $description
      image: $image
      maxUserNumber: $maxUserNumber
      greatImage: $greatImage
      street: $street
      begin_date: $begin_date
      end_date: $end_date
      price: $price
    ) {
      id
    }
  }
`;
export class CreateArticle extends Component {
  state = {
    adresse: "",
    title: "",
    description: "",
    image: "",
    greatImage: "",
    price: 0,
    nbPersons: 2,
    loadigImg: false,
    begin_date: moment(new Date(Date.now())).format("YYYY-MM-DD"),
    end_date: moment(new Date(Date.now()))
      .add(1, "days")
      .format("YYYY-MM-DD")
  };

With the Mutation call:

 <Mutation
        mutation={CREATE_ARTICLE_MUTATION}
        variables={{
          ...this.state,
          maxUserNumber: this.state.nbPersons,
          street: this.state.adresse
        }}
      >

And the backendMutation:

async createArticle(parent, args, ctx, info) {

    if (!ctx.request.userId) {
      throw new Error("Vous devez être connecté");
    }


    const article = await ctx.db.mutation.createArticle(
      {
        data: {
          user: {
            connect: {
              id: ctx.request.userId
            }
          },
          ...args
        }
      },
      info
    );

    return article;
  },

When I use my createArticle mutation I have an error that specified:

Reason 'end_date' field is not defined in the type ArticleCreateInput

But, when I log the args on my mutation, I have all my fields!

1
can you also add the mutation logic you are using? Client side too :)Bryan
I added this in the description above. Thanks for your helpThibault Jp
GraphQL converts identifiers to camelcase by default. That's most likely your problem. begin_date, end_date -> beginDate, endDatecolemerrick

1 Answers

0
votes

As I understand this is a graphql runtime issue, therefore try to temporarily replace the DateTime type with either a string or a number (if you rely on an epoch). A possible problem is that you are using this non standard DateTime scalar, meaning you need a resolver to convert this custom type which one apollo will use to serialize it. If for some reason this converter is broken, it might return null or undefined which would produce the error you do have.