• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

EcrituresNumeriques / stylo / 15922807606

27 Jun 2025 09:18AM UTC coverage: 39.241% (+0.04%) from 39.201%
15922807606

push

github

web-flow
Crée des requêtes de prévisualisation d'articles/corpus (#1592)

Co-authored-by: Thomas Parisot <thom4parisot@users.noreply.github.com>
Co-authored-by: Guillaume Grossetie <ggrossetie@yuzutech.fr>

570 of 800 branches covered (71.25%)

Branch coverage included in aggregate %.

90 of 250 new or added lines in 17 files covered. (36.0%)

137 existing lines in 8 files now uncovered.

5638 of 15020 relevant lines covered (37.54%)

2.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.55
/front/src/helpers/graphQL.js
1
import { useSelector } from 'react-redux'
1✔
2

3
import { print } from 'graphql/language/printer'
1✔
4

5
import { applicationConfig } from '../config.js'
1✔
6

7
const corsStrategy = APP_ENVIRONMENT === 'prod' ? 'same-origin' : 'include'
1!
8

9
/**
1✔
10
 * @typedef {import('graphql/language/ast').DocumentNode} DocumentNode
1✔
11
 */
1✔
12

13
/**
1✔
14
 * @param {object} config request configuration
1✔
15
 * @param {string} config.query request query (as string)
1✔
16
 * @param {'omit' | 'same-origin' | 'include'} config.credentials request query (as string)
1✔
17
 * @param {{[string: key]: value}|undefined} config.variables request variables
1✔
18
 * @param {string} config.sessionToken session token (for authentication)
1✔
19
 * @param {'fetch'|'mutate'} config.type request type (either fetch or mutate)
1✔
20
 * @returns {Promise<string|object>}
1✔
21
 */
1✔
22
async function executeRequest({
25✔
23
  query,
25✔
24
  variables,
25✔
25
  sessionToken,
25✔
26
  type = 'fetch',
25✔
27
  credentials = 'omit',
25✔
28
}) {
25✔
29
  const response = await fetch(applicationConfig.graphqlEndpoint, {
25✔
30
    method: 'POST',
25✔
31
    mode: 'cors',
25✔
32
    credentials,
25✔
33
    headers: {
25✔
34
      'Content-Type': 'application/json',
25✔
35
      Accept: 'application/json',
25✔
36
      // Authorization header is provided only when we have a token
25✔
37
      ...(sessionToken ? { Authorization: `Bearer ${sessionToken}` } : {}),
25!
38
    },
25✔
39
    body: JSON.stringify({
25✔
40
      query,
25✔
41
      variables,
25✔
42
    }),
25✔
43
  })
25✔
44

45
  // GraphQL server always returns a 200/OK (even when there are one or more errors)
25✔
46
  if (!response.ok) {
25!
NEW
47
    throw new ErrorResponse({
×
NEW
48
      statusText: response.statusText,
×
NEW
49
      status: response.status,
×
NEW
50
      //data: await response.text(),
×
NEW
51
    })
×
UNCOV
52
  }
×
53

54
  const body = await response.json()
25✔
55
  if (body.errors) {
25✔
56
    throw new GraphQLError(body.errors)
2✔
57
  }
2✔
58

59
  return body.data
23✔
60
}
23✔
61

62
export class ErrorResponse extends Error {
1✔
63
  constructor({ status, statusText, data }) {
1✔
NEW
64
    super()
×
NEW
65
    this.status = status
×
NEW
66
    this.statusText = statusText
×
NEW
67
    this.data = data
×
NEW
68
    this.message = statusText
×
NEW
69
  }
×
70
}
1✔
71

72
export class GraphQLError extends Error {
1✔
73
  constructor(errors) {
1✔
74
    super()
2✔
75
    this.errors = errors
2✔
76
    this.status = errors?.at(0).extensions?.http?.status || 400
2!
77
    this.code = errors?.at(0).extensions?.code
2✔
78
    this.message = errors?.at(0).message
2✔
79
  }
2✔
80
}
1✔
81

82
export function useGraphQLClient() {
1✔
83
  const sessionToken = useSelector((state) => state.sessionToken)
54✔
84
  return {
54✔
85
    query: ({ query, variables, type = 'fetch', withCredentials = false }) =>
54✔
86
      executeQuery({
9✔
87
        query,
9✔
88
        variables,
9✔
89
        sessionToken,
9✔
90
        type,
9✔
91
        credentials: withCredentials === false ? 'omit' : corsStrategy,
9✔
92
      }),
54✔
93
  }
54✔
94
}
54✔
95

96
/**
1✔
97
 * @param {object} context query context
1✔
98
 * @param {DocumentNode|string} context.query request query (as AST or string)
1✔
99
 * @param {'omit' | 'same-origin' | 'include'} context.credentials request variables
1✔
100
 * @param {{[string: key]: any}|undefined} context.variables request variables
1✔
101
 * @param {string} context.sessionToken session token (for authentication)
1✔
102
 * @param {'fetch'|'mutate'} context.type request type (either fetch or mutate)
1✔
103
 * @returns {Promise<string|{[key: string]: any}>}
1✔
104
 * @throws Error if something went wrong
1✔
105
 */
1✔
106
export function executeQuery({
1✔
107
  query: queryOrAST,
25✔
108
  variables,
25✔
109
  sessionToken,
25✔
110
  credentials = 'omit',
25✔
111
  type = 'fetch',
25✔
112
}) {
25✔
113
  const query = typeof queryOrAST === 'string' ? queryOrAST : print(queryOrAST)
25✔
114
  return executeRequest({ query, variables, sessionToken, type, credentials })
25✔
115
}
25✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc