unjs / ofetch

😱 A better fetch API. Works on node, browser and workers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GraphQL Support

mrnasil opened this issue · comments

Describe the feature

I am wondering if GraphQL support can be added without going into too much detail.

const query = `
  query GetUser($id: ID!) {
    user(id: $id) {
      id
      name
      email
    }
  }
`;

const variables = {
  id: '123',
};

ofetch('https://your-graphql-endpoint.com/graphql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    query: query,
    variables: variables,
  }),
});


### Additional information

- [ ] Would you be willing to help implement this feature?

Hi @mrnasil

here 's the way I create a basic graphql client in my utils folder

export const $graphqlClient = async (query: string, variables?: object) => {
  const  { data, errors } = await $fetch( ~SERVER_ENDPOINT~, {
    method: 'POST',
    headers: {
      "Content-Type": "application/json",
      "x-secret": ~SECRET-TOKEN~
    },
    body: {
      query,
      variables
    }
  })
  return { data, errors }
}

Hi @mrnasil

here 's the way I create a basic graphql client in my utils folder

export const $graphqlClient = async (query: string, variables?: object) => {
  const  { data, errors } = await $fetch( ~SERVER_ENDPOINT~, {
    method: 'POST',
    headers: {
      "Content-Type": "application/json",
      "x-secret": ~SECRET-TOKEN~
    },
    body: {
      query,
      variables
    }
  })
  return { data, errors }
}

"Thank you very much for your response. I also use it in the same way. However, I believe it would be more appropriate for it to be included within ofetch as an optional feature.