25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

70 satır
1.4 KiB

  1. import {
  2. ApolloClient,
  3. InMemoryCache,
  4. gql,
  5. useQuery,
  6. createHttpLink,
  7. } from "@apollo/client";
  8. import ApolloConfig from "../config/apollo-config";
  9. import fetch from "cross-fetch";
  10. function initApollo(token) {
  11. const httpLink = createHttpLink({
  12. uri: ApolloConfig.graphql_uri,
  13. fetch: fetch,
  14. headers:
  15. token != ""
  16. ? {
  17. Authorization: `Bearer ${token}`,
  18. }
  19. : null,
  20. });
  21. return new ApolloClient({
  22. ssrMode: false,
  23. cache: new InMemoryCache({
  24. addTypename: false,
  25. }),
  26. link: httpLink,
  27. });
  28. }
  29. async function query(query, token = "", variables = {}, cache = false) {
  30. const client = initApollo(token);
  31. var res;
  32. try {
  33. var sql = await client.query({
  34. query: gql`
  35. ${query}
  36. `,
  37. variables: variables,
  38. fetchPolicy: cache ? "cache-first" : "no-cache",
  39. });
  40. res = { STATUS: 1, DATA: sql.data };
  41. } catch (e) {
  42. res = { STATUS: 0, DATA: e };
  43. }
  44. return res;
  45. }
  46. async function mutation(mutation, token = "", variables = {}) {
  47. const client = initApollo(token);
  48. var res;
  49. try {
  50. var sql = await client.mutate({
  51. mutation: gql`
  52. ${mutation}
  53. `,
  54. variables: variables,
  55. });
  56. res = { STATUS: 1, DATA: sql.data };
  57. } catch (e) {
  58. res = { STATUS: 0, DATA: e };
  59. }
  60. return res;
  61. }
  62. module.exports = {
  63. query: query,
  64. mutation: mutation,
  65. };