0

I've created a graphql server using graphql-yoga and using volatile as the database for my application. So far posting works good, i'm facing issues trying to get the data i've added to the database.

Here is my code so you can help me figuring out what i'm doing wrong.

In my schema I have the next.

type Query{
memes:[Meme!]!
meme(id:ID): Meme
text:String!

}

the memes query is the one failing.

The query itself is the next.

Query:{
    memes:()=>fetch(`${VOLATILE_URL}`).then(res=>console.log(res.json())),
    meme:(parent,args)=>memes.find(meme=>meme.id===args.id),
}

And this is the way I'm adding the data.

let idCount = 0;
const memes = [];
type Mutation{
createMeme(url:String!, text:String!):Meme
}
Mutation:{
    createMeme:(parent,args)=>{
        const meme = {
            id:`${idCount++}`,
            url:args.url,
            text:args.text,
        }
        memes.push(meme);
        fetch(`${VOLATILE_URL}&val={"memes_list":${memes}}`)
            .then(res=>console.log(res));

        //fetch(`${VOLATILE_URL}${meme.id}&val={"id":${meme.id},"url":${meme.url},"text":${meme.text}}`)
          //  .then(res=> console.log(res));
    },
}

Now when accessing https://volatile.wtf/?key=memes the response is my object with an array of objects. But doing the request this way.

query{
  memes{
    id
    url
    text
  }
}

I get Cannot return null for non-nullable field Query.memes. I'm new to graphql so I really don't know what is the way to fetch this data. Thanks in advance.

Jaime Cuellar
  • 464
  • 1
  • 5
  • 20

1 Answers1

2

this resolver ...

memes:()=>fetch(`${VOLATILE_URL}`).then(res=>console.log(res.json())),

returns nothing (resolver should)

if you need to debug responses, call return at the end

memes:()=>fetch(`${VOLATILE_URL}`).then(res=> { 
  let response = res.json();
  console.log(response);
  return response;
} ),
xadm
  • 8,219
  • 3
  • 14
  • 25
  • Thank you! This worked with the request itself, but now i'm getting error `invalid json response body at https://volatile.wtf/?key=memes reason: Unexpected token o in JSON at position 15` – Jaime Cuellar Apr 13 '20 at 21:13
  • explore response ... resolver should return an array of Meme types/objects – xadm Apr 13 '20 at 21:28