-
-
Save jgcmarins/5d9b9968553cecf35762ae8088ef684c to your computer and use it in GitHub Desktop.
New logger event-based relay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Environment, | |
Network, | |
RecordSource, | |
Store, | |
} from 'relay-runtime'; | |
import { RelayTransactionLogger } from './RelayTransactionLogger'; | |
// Define a function that fetches the results of an operation (query/mutation/etc) | |
// Create a network layer from the fetch function | |
const network = Network.create(/* your fetchQuery */); | |
const store = new Store(new RecordSource()) | |
const environment = new Environment({ | |
network, | |
store, | |
log: RelayTransactionLogger // log function | |
}); | |
export default environment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
OperationDescriptor, | |
RequestParameters, | |
Variables, | |
GraphQLResponse, | |
} from '@types/relay-runtime'; | |
export type LogEvent = | |
{ | |
name: 'queryresource.fetch', | |
operation: OperationDescriptor, | |
// FetchPolicy from relay-experimental | |
fetchPolicy: string, | |
// RenderPolicy from relay-experimental | |
renderPolicy: string, | |
hasFullQuery: boolean, | |
shouldFetch: boolean, | |
} | |
| { | |
name: 'execute.info', | |
transactionID: number, | |
info: any, | |
} | |
| { | |
name: 'execute.start', | |
transactionID: number, | |
params: RequestParameters, | |
variables: Variables, | |
} | |
| { | |
name: 'execute.next', | |
transactionID: number, | |
response: GraphQLResponse, | |
} | |
| { | |
name: 'execute.error', | |
transactionID: number, | |
error: Error, | |
} | |
| { | |
name: 'execute.complete', | |
transactionID: number, | |
} | |
| { | |
name: 'execute.unsubscribe', | |
transactionID: number, | |
}; | |
const unlogged: {[key: number]: LogEvent}[] = []; | |
export const RelayTransactionLogger = (event: LogEvent) => { | |
if (event.name === 'execute.start') { | |
const { transactionID } = event; | |
unlogged[transactionID] = [event]; | |
console.group('RELAY: ', unlogged[transactionID]); | |
console.groupEnd(); | |
} | |
if (event.name === 'execute.next') { | |
const { transactionID } = event; | |
unlogged[transactionID] = [...unlogged[transactionID], event]; | |
console.group('RELAY: ', unlogged[transactionID]); | |
console.groupEnd(); | |
} | |
if (event.name === 'execute.error' || event.name === 'execute.complete' || event.name === 'execute.unsubscribe') { | |
const { transactionID } = event; | |
console.group('RELAY: ', unlogged[transactionID]); | |
delete unlogged[transactionID]; | |
console.groupEnd(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment