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.
 
 

71 satır
1.4 KiB

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