vendredi 12 mars 2021

GraphQL mutation create document which is array of object

I am making a server with GraphQL and this is my schema:

const PostType = new GraphQLObjectType({
  name: 'Post',
  fields: () => ({
    id: {
      type: GraphQLID,
    },
    caption: {type: GraphQLString},
    createdAt: {type: GraphQLString},
    likeCount: {type: GraphQLInt},
    displayName: {type: GraphQLString},
    likedList: {
      type: new GraphQLList(GraphQLString),
    },
    uid: {
      type: GraphQLID,
    },
    uploadedMediaList: {
      type: new GraphQLList(MediaListType),
      resolve(parent, args) {
        return _.filter(mediaListDB, {postId: parent.id});
      },
    },
  }),
});

const MediaListType = new GraphQLObjectType({
  name: 'MediaList',
  fields: () => ({
    id: {
      type: GraphQLID,
    },
    mediaUrl: {type: GraphQLString},
    type: {type: GraphQLString},
    postId: {type: GraphQLID},
  }),
});

const Mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
    createPost: {
      type: PostType,
      args: {
        id: {type: GraphQLID},
        caption: {type: new GraphQLNonNull(GraphQLString)},
        createdAt: {type: new GraphQLNonNull(GraphQLString)},
        likeCount: {type: new GraphQLNonNull(GraphQLInt)},
        displayName: {type: new GraphQLNonNull(GraphQLString)},
        likedList: {type: new GraphQLNonNull(new GraphQLList(GraphQLString))},
        uid: {type: new GraphQLNonNull(GraphQLString)},
        uploadedMediaList: {
          type: new GraphQLNonNull(new GraphQLList(MediaListType)),
        },
      },
      resolve(parent, args) {
        const {
          id,
          caption,
          createdAt,
          likeCount,
          displayName,
          likedList,
          uid,
          uploadedMediaList,
        } = args;

        return postDB.unshift({
          id,
          caption,
          createdAt,
          likeCount,
          displayName,
          likedList,
          uid,
          uploadedMediaList,
        });
      },
    },
  },
});

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    post: {
      type: PostType,
      args: {
        id: {type: GraphQLID},
      },
      resolve(parent, args) {
        return _.find(postDB, {id: args.id});
      },
    },
    posts: {
      type: new GraphQLList(PostType),
      resolve(parent, args) {
        return postDB;
      },
    },
    mediaList: {
      type: MediaListType,
      args: {
        id: {type: GraphQLID},
      },
      resolve(parent, args) {
        return _.find(mediaListDB, {id: args.id});
      },
    },
    mediaLists: {
      type: new GraphQLList(MediaListType),
      resolve(parent, args) {
        return mediaListDB;
      },
    },
  },
});

module.exports = new GraphQLSchema({
  query: RootQuery,
  mutation: Mutation
});

As you can see, schema PostType has an property is an array of MediaListType items, each of items is an object. Now in the Mutation, I got a query createPost and I define that the uploadedMediaList is a GraphQLList of MediaList and I want my query like this

mutation {
  createPost(id: "101", createdAt: "1 25 13/03/2021", caption: "Hope it work", likeCount: 1000, displayName: "Connor McKnight", likedList: ["Nguoi thu nhat", "Ai do nua"], uid: "5y7325y4732", uploadedMediaList: [{id: "99", mediaUrl: "fdsnjakfndjksanfdjksanfjkdsanfkdsa", type: "dummy type", postId: "101"}]) {
    likeCount
    id
    createdAt
  }
}

This is the error I got:

{
  "errors": [
    {
      "message": "The type of Mutation.createPost(uploadedMediaList:) must be Input Type but got: [MediaList]!."
    }
  ]
}

Please help me tackle it, thank you so much and have a good day




Aucun commentaire:

Enregistrer un commentaire