apollo-client | production ready caching GraphQL client for every UI | GraphQL library
kandi X-RAY | apollo-client Summary
kandi X-RAY | apollo-client Summary
Apollo Client is a fully-featured caching GraphQL client with integrations for React, Angular, and more. It allows you to easily build UI components that fetch data via GraphQL.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of apollo-client
apollo-client Key Features
apollo-client Examples and Code Snippets
import { useApolloClient } from '@apollo/client'
client
.mutate({
mutation:MUTATION,
variables: {
Input: input,
},
})
.catch((error) =>
import { client } from '../apollo.js';
import { onMount } from "svelte";
import { gql } from '@apollo/client'
export const COMPANY_LIST = gql`
query {
listCompanies{
companyName
}
}
`;
async functio
export const cache: InMemoryCache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
getColor()
return colorVar();
}
}
}
}
});
export const colorVar = cache.makeVar('red')
npm install --save vue-apollo graphql apollo-boost
yarn add vue-apollo graphql apollo-boost
import { ApolloClient } from 'apollo-client'
import { InMemoryCache, NormalizedCacheObject } from 'apollo-cache-inmemory'
import {
ApolloClient,
InMemoryCache,
NormalizedCacheObject,
} from '@apollo/client'
<
import React from 'react'
import { useQuery, gql } from '@apollo/client'
import close from '../public/close.svg'
/**
*
*
* Just a typical menu you might see on a CMS-driven site. It takes in a couple of props to move state around.
*
import {
ApolloProvider as ApolloProvider2,
Query,
} from '@apollo/react-components'
import {
ApolloProvider,
ApolloClient,
HttpLink,
InMemoryCache,
useQuery,
gql,
} from '@apollo/client'
const modifyDataLink = new ApolloLink((operation, forward) => {
return forward(operation).map(response => {
// Modify response.data...
return response;
});
});
// use with apollo-client
const link = modifyDataLink.conca
Community Discussions
Trending Discussions on apollo-client
QUESTION
Here, I am creating new ApolloClient with graphql
...ANSWER
Answered 2022-Mar-27 at 11:59the problem is with you are using customHook and that is the promise for graphql what it does is return the instance of that current runtime and it is undefined at that time all you can do is simply use in the component that you want to use why are you complexing things.
QUESTION
I'm writing a toy app to learn more about Serverless Framework and AWS AppSync etc.
I'm trying to do TDD as much as possible. I'm using mock-apollo-client
to mock the ApolloClient
, and I've run into a problem. When trying to write a test to make sure the arguments to the query are passed, the test always returns a 401 Unauthorized error. It seems as though the real end point is still being called, because when a valid x-api-key
is added to the instantiation of the ApolloClient, the test returns the real value from the AppSync server, and not the mock value I'm expecting. I'm using a mock, not spy, so I'm not expecting the real end point to actually be hit. Furthermore When I do add a valid x-api-key
the test fails because the function is never called.
ANSWER
Answered 2022-Mar-25 at 08:52mock-apollo-client always use the with ApolloProvider
, so that you pass the mock apollo client via React context Provider to descendant components.
However, your code cannot pass the mock apollo client to the component in this way. Your code initiates requests directly from the Apollo Client. We need to intercept these GraphQL requests. There are several ways to do this such as msw. However, I'll continue to use the mock-apollo-client
library to demonstrate.
You need to mock ApolloClient
class of the @apollo/client
module. We need to use Mocking Partials, we don't want to mock other things exported from @apollo/client
. Since the mock-apollo-client
library already provides createMockClient
function to create mocked apollo client, we don't need to mock by ourself.
An working example:
recipes.ts
:
QUESTION
I'm attempting to create an apollo client
plugin for a Nuxt 3
application. It's currently throwing an error regarding a package called ts-invariant
:
ANSWER
Answered 2022-Jan-07 at 01:52Solved by including @apollo/client
and ts-invariant/process
into the nuxt build transpile like so:
QUESTION
Contrast in "apollo-client in next.js" approaches chosen in next-with-apollo
npm library and the approach shown in next.js docs.
The link of approach chosen by next.js for apollo client: https://github.com/vercel/next.js/blob/canary/examples/with-apollo/lib/apolloClient.js
- no third-party library next-with-apollo is used
- no get-data-from-tree is used
- Moreover i found this approach more meaningful and elegant inner-working of client side redering and SSR of apollo-client in next.js. I strongly like this
Some cons in next-with-apollo
approach
- in next-with-apollo docs, it is stated that in withApollo API, the parameter getDataFromTree of intialState defaults to undefined by implementaion and its stated "It's recommended to never set this prop, otherwise the page will be a lambda without Automatic Static Optimization "
- get-initial-props is used which is not recommended by next.js for optimization reasons general thing. Why to consider third party library if there is non-third party way and suggested officially unless it has drawbacks?
It made me really curious to see many are using next-with-apollo
and rarely seen the usage of the approach shown in next.js docs? I'm curious whether approach in next.js docs has any drawbacks (which i strong think of not having any)?
- does the approach shown in next.js has some drawbacks?
- does next-with-apollo has more efficiencies? if so what r those more efficiencies for which it is wise not to choose next.js doc approach. I wanna be sure that if i'm rejecting next.js doc approach (currently im choosing it) i'm not doing any wrong
So which is better for both client-side data fetching and server-data fetching to support both CSR and SRR?
...ANSWER
Answered 2021-Aug-20 at 17:34I found the answer by posting in next.js community:
Here it goes:
next.js doc's apollo examples avoid using getDataFromTree because it traverses the react tree twice in order to trigger all the queries and collect their result afterwards.
The drawback of using the approach on the next.js doc's apollo examples is since you don't use
getDataFromTree
, you have no way to know which queries your inner components are using. So you need to remember to prefetch everything you need ongetStaticProps
/getServerSideProps
and match the exact same queries/variablesnext.js doc's apollo examples way is recommended instead of
getInitialProps
so I would always use them unless one has some very specific reason not to
QUESTION
When I see sample apps which uses Apollo-client
I see following constructor.When I read HttpLink document, it seems that graphql endpoint must be set as uri
but in following code, I couldn't find them.
ANSWER
Answered 2022-Feb-16 at 17:53The HttpLink
constructor option uri
has a default value of /graphql
. So as long as that is the correct path it will work without passing a uri
option into the constructor.
So your example code:
QUESTION
My React app is using Webpack + Babel. When I compile in development everything works perfectly well.
When I bundle for production ("npm run build") and upload the bundle in prod, an error appear in the console:
Why? I found a similar question but didn't find an answer : related stackoverflow question
Here's my webpack.prod.js
config:
ANSWER
Answered 2021-Dec-30 at 17:37Pointing an alias to a node module is an error. Just remove your resolve
entry and everything should run fine.
QUESTION
I have a React project using Babel and Webpack. Recently I realized that my webpack wasn't "hot loading" anymore when I make a change in my project files. (this cause me some trouble, anyhow)
I audited my npm dependencies and had 60 vulnerabilities with 9 high and 2 critical. I thought this should be taken care of.
Now, I tried to install the package that seems to broke things (using npm audit) but to no avail. I still got 31 vulnerabilities even after trying to install a different version of React Script.
Now, if I try to start my app, webpack doesn't compile saying "Cannot find module '@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining'"
I tried to install the Babel dependencies but every time a new one comes up. I know Babel just recently updated to 7.16 (October 31, 2021). Is this why my problems started?
How should I go about resolving all those dependencies issues? I feel it's a never ending instance of install a new packages that just break another one...
...ANSWER
Answered 2021-Dec-13 at 23:38QUICK UPDATE
I made progress over my dependencies vulnerabilities. The main issue was a package that was interfering with the others. But I didn't clean my packages in a long time so it was impossible to know which one.
Here's my process: (to check what needs to be updated)
QUESTION
I'm trying to implement a global error handling for a React application, which uses Apollo Client 3.4.8
.
I'm setting up the App and Apollo like this:
...ANSWER
Answered 2021-Oct-19 at 01:22So, the issue here is that we can't replace a Link in the chain and when doing this:
client.setLink(from([errorLink, client.link]));
we could be adding multiple error handlers.
The solution is to apply again all the links we need + the new error handler we want to set
QUESTION
I'm trying to get started with both GraphQL and Svelte using Apollo Server, but all I get is this error "ctx[random number].data is undefined". I also tried svelte {#async} but got the same error. What I'm doing wrong and how to make it works?
...ANSWER
Answered 2021-Sep-06 at 18:11Follow the example on Svelte Apollo Client's page
QUESTION
I'm facing next problem: I have a useQuery hook for getting usersData
GET_USER_INFO useQuery ...ANSWER
Answered 2021-Sep-01 at 17:26You can use the retry link to run the query again.
You should chain your links in a way that the retry link is the most outer link so that you can wait for your error link to fetch the token. Check out the custom strategies, here you could react to a specific status code in the error object or listen to a custom property, that the error link could attach to the operation
object. This way you can even prevent a refetch if the token refresh failed (e.g. because the refetch token also timed out).
The links are also all open source so if chaining them doesn't really work for you, you could also develop your own refetch+retry link by combining the code of both links.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install apollo-client
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page