First of all sorry for my english. So I am trying to fetch some data from GraphQL server and have several problems, that's my code:
import { createHttpLink } from 'apollo-link-http';
import { gql } from '@apollo/client';
import { ApolloClient, InMemoryCache } from '@apollo/client';
import 'babel-polyfill';
const httpLink = createHttpLink({
uri: 'https://api.spacex.land/graphql/',
headers: {
authorization: 'Bearer MY_TOKEN',
},
});
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
connectToDevTools: true,
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
}
});
async function fetchedData(params) {
const fetched = await client.query({
query: gql`
query {
launchesPast(limit: 10) {
mission_name
launch_date_local
launch_site {
site_name_long
}
links {
article_link
video_link
}
rocket {
rocket_name
first_stage {
cores {
flight
core {
reuse_count
status
}
}
}
second_stage {
payloads {
payload_type
payload_mass_kg
payload_mass_lbs
}
}
}
ships {
name
home_port
image
}
}
}
`,
fetchPolicy: "network-only",
variables: null
});
return fetched;
}
export default fetchedData().then((res) => console.log(res));
Browser tab Network Browser tab Network
Browser tab Response:
{"errors":[{"message":"query exceeds complexity limit","locations":[{"line":1,"column":1}],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}]}
Browser tab console:
POST https://api.spacex.land/graphql/ 400 (Bad Request);
And for example the same query via GraphQLClient.request() is working.
import 'babel-polyfill';
import { GraphQLClient, gql } from 'graphql-request'
async function fetchedData() {
const endpoint = 'https://api.spacex.land/graphql/'
const graphQLClient = new GraphQLClient(endpoint, {
headers: {
authorization: 'Bearer MY_TOKEN',
},
})
const query = gql`query {
launchesPast(limit: 10) {
mission_name
launch_date_local
launch_site {
site_name_long
}
links {
article_link
video_link
}
rocket {
rocket_name
first_stage {
cores {
flight
core {
reuse_count
status
}
}
}
second_stage {
payloads {
payload_type
payload_mass_kg
payload_mass_lbs
}
}
}
ships {
name
home_port
image
}
}
}
`
const data = await graphQLClient.request(query)
console.log(JSON.stringify(data, undefined, 2))
return data;
}
export default fetchedData().catch((error) => console.error(error));
Browser tab Network Browser tab Network
And Browser tab response is correct