MCPcopy
hub / github.com/dgraph-io/dgraph / toFastJSON

Method toFastJSON

query/outputnode.go:1180–1225  ·  view source on GitHub ↗
(ctx context.Context, l *Latency, field gqlSchema.Field)

Source from the content-addressed store, hash-verified

1178}
1179
1180func (sg *SubGraph) toFastJSON(ctx context.Context, l *Latency, field gqlSchema.Field) ([]byte,
1181 error) {
1182 encodingStart := time.Now()
1183 defer func() {
1184 l.Json = time.Since(encodingStart)
1185 }()
1186
1187 enc := newEncoder()
1188 defer func() {
1189 // Put encoder's arena back to arena pool.
1190 arenaPool.Put(enc.arena)
1191 enc.alloc.Release()
1192 }()
1193
1194 var err error
1195 n := enc.newNode(enc.idForAttr("_root_"))
1196 for _, sg := range sg.Children {
1197 err = processNodeUids(n, enc, sg)
1198 if err != nil {
1199 return nil, err
1200 }
1201 }
1202 enc.fixOrder(n)
1203
1204 // According to GraphQL spec response should only contain data, errors and extensions as top
1205 // level keys. Hence we send server_latency under extensions key.
1206 // https://facebook.github.io/graphql/#sec-Response-Format
1207
1208 // if there is a GraphQL field that means we need to encode the response in GraphQL form,
1209 // otherwise encode it in DQL form.
1210 if field != nil {
1211 // if there were any GraphQL errors, we need to propagate them back to GraphQL layer along
1212 // with the data. So, don't return here if we get an error.
1213 err = sg.toGraphqlJSON(newGraphQLEncoder(ctx, enc), n, field)
1214 } else if err = sg.toDqlJSON(enc, n); err != nil {
1215 return nil, err
1216 }
1217
1218 // Return error if encoded buffer size exceeds than a threshold size.
1219 if uint64(enc.buf.Len()) > maxEncodedSize {
1220 return nil, fmt.Errorf("while writing to buffer. Encoded response size: %d"+
1221 " is bigger than threshold: %d", enc.buf.Len(), maxEncodedSize)
1222 }
1223
1224 return enc.buf.Bytes(), err
1225}
1226
1227func (sg *SubGraph) toDqlJSON(enc *encoder, n fastJsonNode) error {
1228 if enc.children(n) == nil {

Callers 1

ToJsonFunction · 0.95

Calls 12

toGraphqlJSONMethod · 0.95
toDqlJSONMethod · 0.95
newEncoderFunction · 0.85
processNodeUidsFunction · 0.85
newGraphQLEncoderFunction · 0.85
PutMethod · 0.80
ReleaseMethod · 0.80
newNodeMethod · 0.80
idForAttrMethod · 0.80
fixOrderMethod · 0.80
LenMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected