Skip to content

Commit

Permalink
Use undici fetch instead of custom implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Aug 13, 2021
1 parent 9816707 commit 182128e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 47 deletions.
5 changes: 5 additions & 0 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ declare interface ObjectConstructor {
keys<T>(obj: T): Array<keyof T>;
}
declare module 'ajv-formats';
declare module 'undici' {
export const fetch: typeof import('cross-fetch').fetch;
export const Request: typeof import('cross-fetch').Request;
export const Response: typeof import('cross-fetch').Response;
}
7 changes: 3 additions & 4 deletions packages/handlers/odata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ import {
import { parseResolveInfo, ResolveTree, simplifyParsedResolveInfoFragmentWithType } from 'graphql-parse-resolve-info';
import DataLoader from 'dataloader';
import { parseResponse } from 'http-string-parser';
import { nativeFetch } from './native-fetch';
import { pascalCase } from 'pascal-case';
import { EventEmitter } from 'events';
import { parse as parseXML } from 'fast-xml-parser';
import { pruneSchema } from '@graphql-tools/utils';
import { Request, Response } from 'cross-fetch';
import { PredefinedProxyOptions } from '@graphql-mesh/store';
import { env } from 'process';
import { fetch as nativeFetch, Request, Response } from 'undici';

const SCALARS = new Map<string, string>([
['Edm.Binary', 'String'],
Expand Down Expand Up @@ -174,7 +173,7 @@ export default class ODataHandler implements MeshHandler {
})
: this.config.customFetch;
} else {
fetch = getCachedFetch(this.cache);
fetch = nativeFetch;
}

const { baseUrl: nonInterpolatedBaseUrl, operationHeaders } = this.config;
Expand Down Expand Up @@ -924,7 +923,7 @@ export default class ODataHandler implements MeshHandler {
// If entitySetPath is not available, take first parameter as entity
// The first segment of the entity set path must match the binding parameter name
// (see: http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/odata-csdl-xml-v4.01.html#_Toc38530388)
entitySetPath = entitySetPath && entitySetPath.split("/")[0] || parameterName;
entitySetPath = (entitySetPath && entitySetPath.split('/')[0]) || parameterName;
if (entitySetPath === parameterName) {
boundEntityTypeName = getTypeNameFromRef({
typeRef: parameterTypeRef,
Expand Down
33 changes: 0 additions & 33 deletions packages/handlers/odata/src/native-fetch.ts

This file was deleted.

30 changes: 20 additions & 10 deletions packages/handlers/odata/test/handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('odata', () => {
});
const source = await handler.getMeshSource();
const personType = source.schema.getType('IPerson') as GraphQLInterfaceType;
const getFriendsTripsFunction = personType.getFields()['GetFriendsTrips'];
const getFriendsTripsFunction = personType.getFields().GetFriendsTrips;
expect(getFriendsTripsFunction.args).toHaveLength(2);
const personArg = getFriendsTripsFunction.args.find(arg => arg.name === 'person');
expect(personArg).not.toBeFalsy();
Expand Down Expand Up @@ -301,11 +301,16 @@ describe('odata', () => {
],
};
let sentRequest: Request;
let sentBody: any;
addMock(correctUrl, async request => {
sentRequest = request;
const bodyObj = JSON.parse(request.body as any);
bodyObj['@odata.type'] = 'Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person';
return new Response(JSON.stringify(bodyObj));
sentBody = await request.json();
return new Response(
JSON.stringify({
...sentBody,
'@odata.type': 'Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person',
})
);
});
const handler = new ODataHandler({
name: 'TripPin',
Expand Down Expand Up @@ -337,7 +342,7 @@ describe('odata', () => {
expect(graphqlResult.errors).toBeFalsy();
expect(sentRequest!.method).toBe(correctMethod);
expect(sentRequest!.url).toBe(correctUrl);
expect(JSON.parse(sentRequest!.body as any)).toStrictEqual(correctBody);
expect(sentBody).toStrictEqual(correctBody);
});
it('should generate correct HTTP request for deleting an entity', async () => {
addMock('https://services.odata.org/TripPinRESTierService/$metadata', async () => new Response(TripPinMetadata));
Expand Down Expand Up @@ -383,11 +388,16 @@ describe('odata', () => {
LastName: 'King',
};
let sentRequest: Request;
let sentBody: any;
addMock(correctUrl, async request => {
sentRequest = request;
const returnBody = JSON.parse(request.body as any);
returnBody['@odata.type'] = 'Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person';
return new Response(JSON.stringify(returnBody));
sentBody = await request.json();
return new Response(
JSON.stringify({
...sentBody,
'@odata.type': 'Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person',
})
);
});
const handler = new ODataHandler({
name: 'TripPin',
Expand Down Expand Up @@ -420,7 +430,7 @@ describe('odata', () => {
expect(graphqlResult.errors).toBeFalsy();
expect(sentRequest!.method).toBe(correctMethod);
expect(sentRequest!.url).toBe(correctUrl);
expect(await sentRequest!.text()).toBe(JSON.stringify(correctBody));
expect(sentBody).toStrictEqual(correctBody);
});
it('should generate correct HTTP request for invoking unbound functions', async () => {
addMock('https://services.odata.org/TripPinRESTierService/$metadata', async () => new Response(TripPinMetadata));
Expand Down Expand Up @@ -568,7 +578,7 @@ describe('odata', () => {

expect(graphqlResult.errors).toBeFalsy();
expect(sentRequest!.method).toBe(correctMethod);
expect(sentRequest!.url).toBe(correctUrl.replace(/'/g, '%27', ));// apostrophe gets percent-encoded
expect(sentRequest!.url).toBe(correctUrl);
});
it('should generate correct HTTP request for invoking unbound actions', async () => {
addMock('https://services.odata.org/TripPinRESTierService/$metadata', async () => new Response(TripPinMetadata));
Expand Down

0 comments on commit 182128e

Please sign in to comment.