What is currently the best way to use Apollo GraphQL with the ORM? #1013
Answered
by
CodeDredd
rbartholomay
asked this question in
Q&A
|
Hi there, I think the ORM is great way to use data inside vuejs/Pinia! I try this and it runs fine in my case. Thanks! |
Answered by
CodeDredd
Mar 2, 2023
Replies: 2 comments
|
Hey :), the best way to use any api with pinia-orm is to use the repository. I am still working on the examples and cookbook so that such questions are clear. Ihaven't tested the code below, but thats how i would do it now. So you have something like this: import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client/core'
// Create the apollo client
export const useApolloClient = () => {
const httpLink = createHttpLink({
// You should use an absolute URL here
uri: 'http://localhost:3020/graphql',
})
// Cache implementation
const cache = new InMemoryCache()
return new ApolloClient({
link: httpLink,
cache,
})
}import { useApolloClient } from `~/composoables/useApolloClient`
import { provideApolloClient, useQuery } from "@vue/apollo-composable";
import gql from 'graphql-tag'
export default class UserRepository extends BaseRepository<User> {
use = User;
api;
constructor(database: Database, pinia?: Pinia) {
super(database, pinia);
this.api = useApolloClient();
provideApolloClient(this.api);
}
graphQL () {
return {
async fetchAll() {
const { result } = useQuery(...);
this.query().save(result);
}
}
}
}in the file then: const userRepo = useRepo(UserRepository);
userRepo.graphQL.fetchAll() |
0 replies
Answer selected by
CodeDredd
|
Nice!!! I will try this! Thanks... |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey :),
the best way to use any api with pinia-orm is to use the repository. I am still working on the examples and cookbook so that such questions are clear.
So normally you would have a
UserModelandUserRepository. In the repository you then initilize through the construct the api client.Ihaven't tested the code below, but thats how i would do it now. So you have something like this: