Start a new topic

How can I get all of the collection data via GraphQL

Hello,

I found one amazing topic about Serverless GraphQL API using Kinvey Flex Functions & Kinvey Data Collections here 

My question is how can I get json response with all of the record elements - id, name, age, etc... and how can I get again json with all of the records in the table?


Thanks in advance!

1 Comment

Hi Gavrangel,


In the article the graphql query is returning a string and that is being set as a response to the Flex function. In order to return a JSON, you just need to return JSON in the resolve function. Here is an example of calling Flex dataStore() module which makes a request to a Kinvey collection and returns JSON:


const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      posts: {
        type: new GraphQLList(PostType),
        resolve(parent, args, context) {
          return context.modules
            .dataStore()
            .collection('posts')
            .find();
        }
      }
    }
  })
});

const graphql = (context, complete, modules) => {
  const graphqlArguments = {
    schema,
    source: context.body.graphString,
    contextValue: { context, complete, modules }
  };

  graphql(graphqlArguments)
    .then(data =>
      complete()
        .setBody(data)
        .done()
    )
    .catch(error =>
      complete()
        .setBody(error)
        .runtimeError()
        .done()
    );
};

flex.functions.register("graphql", graphql);


Regards,

Martin Apostolov

Login or Signup to post a comment