write.mjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import fetch from 'node-fetch'
  2. import { getIntrospectionQuery } from 'graphql'
  3. // TODO: you can get a token by means of using settings --> api-tokens
  4. const token = '';
  5. const apiUrl = 'https://api.graphcdn.io/api'
  6. const originUrl = 'https://trygql.formidable.dev/graphql/basic-pokedex'
  7. // The app { config { input } } can be used to write the graphcdn.YAML file with help of libs like "@atomist/yaml-updater"
  8. async function listOrganizations() {
  9. const response = await fetch(apiUrl, {
  10. method: 'POST',
  11. headers: {
  12. 'Content-Type': 'application/json',
  13. 'graphcdn-token': token,
  14. },
  15. body: JSON.stringify({
  16. query: /* GraphQL */ `
  17. query {
  18. user {
  19. organizations {
  20. id
  21. name
  22. slug
  23. }
  24. }
  25. }
  26. `,
  27. })
  28. })
  29. const result = await response.json()
  30. return result.data.user.organizations
  31. }
  32. async function pushAppConfig(orgId, schema) {
  33. const config = {
  34. name: 'node-write-test',
  35. originUrl,
  36. schema: originUrl,
  37. queryDepthLimit: 20,
  38. ignoreOriginCacheControl: true,
  39. enablePlayground: true,
  40. injectHeaders: true,
  41. headers: {
  42. 'something-to-inject': '1',
  43. },
  44. keyFields: {
  45. types: {
  46. Pokemon: ['id', 'name']
  47. }
  48. },
  49. scopes: {
  50. AUTHENTICATED: 'header:Authorization',
  51. },
  52. rootTypeNames: { query: 'Query' },
  53. rules: [
  54. { description: 'Cache all queries', maxAge: 600, swr: 900, scope: 'AUTHENTICATED', types: ['Query'] },
  55. ],
  56. bypassCacheHeaders: [{ name: 'x-preview-token' }],
  57. }
  58. const result = await fetch(apiUrl, {
  59. method: 'POST',
  60. headers: {
  61. 'Content-Type': 'application/json',
  62. 'graphcdn-token': token,
  63. },
  64. body: JSON.stringify({
  65. query: /* GraphQL */ `
  66. mutation (
  67. $input: Input!
  68. $appName: String!
  69. $schema: IntrospectionQuery
  70. ) {
  71. pushAppConfig(
  72. input: $input
  73. appName: $appName
  74. schema: $schema
  75. allowDeletion: true
  76. )
  77. }
  78. `,
  79. variables: {
  80. input: config,
  81. schema: schema,
  82. appName: config.name,
  83. }
  84. })
  85. })
  86. }
  87. async function createApp(orgId, schema) {
  88. const config = {
  89. name: 'node-write-test',
  90. originUrl,
  91. schema: originUrl,
  92. queryDepthLimit: 20,
  93. ignoreOriginCacheControl: true,
  94. enablePlayground: true,
  95. injectHeaders: true,
  96. headers: {
  97. 'something-to-inject': '1',
  98. },
  99. keyFields: {
  100. types: {
  101. Pokemon: ['id', 'name']
  102. }
  103. },
  104. scopes: {
  105. AUTHENTICATED: 'header:Authorization',
  106. },
  107. rootTypeNames: { query: 'Query' },
  108. rules: [
  109. { description: 'Cache all queries', maxAge: 600, swr: 900, scope: 'AUTHENTICATED', types: ['Query'] },
  110. ],
  111. bypassCacheHeaders: [{ name: 'x-preview-token' }],
  112. }
  113. const result = await fetch(apiUrl, {
  114. method: 'POST',
  115. headers: {
  116. 'Content-Type': 'application/json',
  117. 'graphcdn-token': token,
  118. },
  119. body: JSON.stringify({
  120. query: /* GraphQL */ `
  121. mutation (
  122. $input: Input!
  123. $schema: IntrospectionQuery
  124. $organizationId: String!
  125. ) {
  126. createAppCli(
  127. input: $input
  128. schema: $schema
  129. organizationId: $organizationId
  130. ) {
  131. id
  132. config {
  133. input
  134. }
  135. }
  136. }
  137. `,
  138. variables: {
  139. input: config,
  140. schema: schema,
  141. organizationId: orgId
  142. }
  143. })
  144. })
  145. return await result.json()
  146. }
  147. async function getServices(slug) {
  148. const response = await fetch(apiUrl, {
  149. method: 'POST',
  150. headers: {
  151. 'Content-Type': 'application/json',
  152. 'graphcdn-token': token,
  153. },
  154. body: JSON.stringify({
  155. query: /* GraphQL */`
  156. query ($slug: String!) {
  157. organization(slug: $slug) {
  158. name
  159. apps {
  160. name
  161. updatedAt
  162. config {
  163. input
  164. }
  165. }
  166. }
  167. }
  168. `,
  169. variables: { slug }
  170. })
  171. })
  172. const result = await response.json()
  173. return result.data.organization.apps
  174. }
  175. async function main() {
  176. const introspectionQuery = getIntrospectionQuery()
  177. const introspectionResponse = await fetch(originUrl, {
  178. method: 'POST',
  179. headers: {
  180. 'Content-Type': 'application/json',
  181. },
  182. body: JSON.stringify({
  183. query: introspectionQuery,
  184. })
  185. })
  186. const { data: schema } = await introspectionResponse.json()
  187. const organizations = await listOrganizations();
  188. console.log(organizations)
  189. const result = await createApp(organizations[0].id, schema)
  190. console.log(result)
  191. }
  192. main()